1 /////////////////////////////////////////////////////////////////////////////
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"
18 #include "wx/settings.h"
20 #include "wx/strconv.h"
21 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
23 #include <sys/types.h>
28 #include "wx/gtk1/private.h"
29 #include <gdk/gdkkeysyms.h>
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern void wxapp_install_idle_handler();
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern wxCursor g_globalCursor
;
43 extern wxWindowGTK
*g_delayedFocus
;
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
50 static void wxGtkTextInsert(GtkWidget
*text
,
51 const wxTextAttr
& attr
,
55 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
58 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
61 GdkColor
*colBg
= attr
.HasBackgroundColour()
62 ? attr
.GetBackgroundColour().GetColor()
65 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
69 // ----------------------------------------------------------------------------
70 // "insert_text" for GtkEntry
71 // ----------------------------------------------------------------------------
75 gtk_insert_text_callback(GtkEditable
*editable
,
76 const gchar
*new_text
,
82 wxapp_install_idle_handler();
84 // we should only be called if we have a max len limit at all
85 GtkEntry
*entry
= GTK_ENTRY (editable
);
87 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
89 // check that we don't overflow the max length limit
91 // FIXME: this doesn't work when we paste a string which is going to be
93 if ( entry
->text_length
== entry
->text_max_length
)
95 // we don't need to run the base class version at all
96 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
98 // remember that the next changed signal is to be ignored to avoid
99 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
100 win
->IgnoreNextTextUpdate();
102 // and generate the correct one ourselves
103 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
104 event
.SetEventObject(win
);
105 event
.SetString(win
->GetValue());
106 win
->GetEventHandler()->ProcessEvent( event
);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
117 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
119 if ( win
->IgnoreTextUpdate() )
122 if (!win
->m_hasVMT
) return;
125 wxapp_install_idle_handler();
128 win
->UpdateFontIfNeeded();
130 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
131 event
.SetEventObject( win
);
132 win
->GetEventHandler()->ProcessEvent( event
);
136 //-----------------------------------------------------------------------------
137 // "changed" from vertical scrollbar
138 //-----------------------------------------------------------------------------
142 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
144 if (!win
->m_hasVMT
) return;
147 wxapp_install_idle_handler();
149 win
->CalculateScrollbar();
153 // ----------------------------------------------------------------------------
154 // redraw callback for multiline text
155 // ----------------------------------------------------------------------------
157 // redrawing a GtkText from inside a wxYield() call results in crashes (the
158 // text sample shows it in its "Add lines" command which shows wxProgressDialog
159 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
160 // don't do anything if we're inside wxYield()
162 extern bool wxIsInsideYield
;
165 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
168 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
171 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
173 if ( !wxIsInsideYield
)
175 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
176 _T("infinite recursion in wxgtk_text_draw aborted") );
178 gs_gtk_text_draw(widget
, rect
);
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
189 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
190 EVT_CHAR(wxTextCtrl::OnChar
)
192 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
193 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
194 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
195 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
196 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
198 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
199 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
200 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
201 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
202 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
205 void wxTextCtrl::Init()
209 SetUpdateFont(false);
211 m_vScrollbar
= (GtkWidget
*)NULL
;
214 wxTextCtrl::~wxTextCtrl()
218 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
220 const wxString
&value
,
224 const wxValidator
& validator
,
225 const wxString
&name
)
229 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
232 bool wxTextCtrl::Create( wxWindow
*parent
,
234 const wxString
&value
,
238 const wxValidator
& validator
,
239 const wxString
&name
)
242 m_acceptsFocus
= true;
244 if (!PreCreation( parent
, pos
, size
) ||
245 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
247 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
252 m_vScrollbarVisible
= false;
254 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
258 // create our control ...
259 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
261 // ... and put into the upper left hand corner of the table
262 bool bHasHScrollbar
= false;
263 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
264 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
265 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
266 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
267 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
271 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
273 // finally, put the vertical scrollbar in the upper right corner
274 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
275 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
276 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
278 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
283 // a single-line text control: no need for scrollbars
285 m_text
= gtk_entry_new();
288 m_parent
->DoAddChild( this );
290 m_focusWidget
= m_text
;
295 gtk_widget_show(m_text
);
299 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
300 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
302 // only initialize gs_gtk_text_draw once, starting from the next the
303 // klass::draw will already be wxgtk_text_draw
304 if ( !gs_gtk_text_draw
)
307 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
309 gs_gtk_text_draw
= draw
;
311 draw
= wxgtk_text_draw
;
317 #if !GTK_CHECK_VERSION(1, 2, 0)
318 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
319 // gtk_editable_insert_text()
320 gtk_widget_realize(m_text
);
325 wxWX2MBbuf val
= value
.mbc_str();
326 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
328 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
333 // Bring editable's cursor uptodate. Bug in GTK.
334 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
338 if (style
& wxTE_PASSWORD
)
341 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
344 if (style
& wxTE_READONLY
)
347 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
352 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
355 // We want to be notified about text changes.
356 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
357 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
359 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
361 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
362 SetDefaultStyle( attrDef
);
368 void wxTextCtrl::CalculateScrollbar()
370 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
372 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
374 if (adj
->upper
- adj
->page_size
< 0.8)
376 if (m_vScrollbarVisible
)
378 gtk_widget_hide( m_vScrollbar
);
379 m_vScrollbarVisible
= false;
384 if (!m_vScrollbarVisible
)
386 gtk_widget_show( m_vScrollbar
);
387 m_vScrollbarVisible
= true;
392 wxString
wxTextCtrl::GetValue() const
394 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
397 if (m_windowStyle
& wxTE_MULTILINE
)
399 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
400 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
406 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
412 void wxTextCtrl::SetValue( const wxString
&value
)
414 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
416 if (m_windowStyle
& wxTE_MULTILINE
)
418 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
419 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
421 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
425 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
428 // GRG, Jun/2000: Changed this after a lot of discussion in
429 // the lists. wxWidgets 2.2 will have a set of flags to
430 // customize this behaviour.
431 SetInsertionPoint(0);
436 void wxTextCtrl::WriteText( const wxString
&text
)
438 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
443 // gtk_text_changed_callback() will set m_modified to true but m_modified
444 // shouldn't be changed by the program writing to the text control itself,
445 // so save the old value and restore when we're done
446 bool oldModified
= m_modified
;
448 if ( m_windowStyle
& wxTE_MULTILINE
)
450 // After cursor movements, gtk_text_get_point() is wrong by one.
451 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
453 // always use m_defaultStyle, even if it is empty as otherwise
454 // resetting the style and appending some more text wouldn't work: if
455 // we don't specify the style explicitly, the old style would be used
456 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
457 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.Len());
459 // we called wxGtkTextInsert with correct font, no need to do anything
460 // in UpdateFontIfNeeded() any longer
463 SetUpdateFont(false);
466 // Bring editable's cursor back uptodate.
467 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
471 // First remove the selection if there is one
472 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
474 // This moves the cursor pos to behind the inserted text.
475 gint len
= GET_EDITABLE_POS(m_text
);
477 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.Len(), &len
);
479 // Bring entry's cursor uptodate.
480 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
483 m_modified
= oldModified
;
486 void wxTextCtrl::AppendText( const wxString
&text
)
488 SetInsertionPointEnd();
492 wxString
wxTextCtrl::GetLineText( long lineNo
) const
494 if (m_windowStyle
& wxTE_MULTILINE
)
496 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
497 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
504 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
509 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
517 return wxEmptyString
;
522 if (lineNo
== 0) return GetValue();
523 return wxEmptyString
;
527 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
529 /* If you implement this, don't forget to update the documentation!
530 * (file docs/latex/wx/text.tex) */
531 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
534 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
536 if ( m_windowStyle
& wxTE_MULTILINE
)
538 wxString text
= GetValue();
540 // cast to prevent warning. But pos really should've been unsigned.
541 if( (unsigned long)pos
> text
.Len() )
547 const wxChar
* stop
= text
.c_str() + pos
;
548 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
559 else // single line control
561 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
568 // index out of bounds
576 long wxTextCtrl::XYToPosition(long x
, long y
) const
578 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
581 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
587 int wxTextCtrl::GetLineLength(long lineNo
) const
589 wxString str
= GetLineText (lineNo
);
590 return (int) str
.Length();
593 int wxTextCtrl::GetNumberOfLines() const
595 if (m_windowStyle
& wxTE_MULTILINE
)
597 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
598 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
603 for (int i
= 0; i
< len
; i
++ )
610 // currentLine is 0 based, add 1 to get number of lines
611 return currentLine
+ 1;
624 void wxTextCtrl::SetInsertionPoint( long pos
)
626 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
630 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
631 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
633 /* we fake a set_point by inserting and deleting. as the user
634 isn't supposed to get to know about this non-sense, we
635 disconnect so that no events are sent to the user program. */
637 gint tmp
= (gint
)pos
;
638 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
639 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
641 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
642 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
644 // bring editable's cursor uptodate. Bug in GTK.
645 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
649 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
651 // Bring editable's cursor uptodate. Bug in GTK.
652 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
656 void wxTextCtrl::SetInsertionPointEnd()
658 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
660 if (m_windowStyle
& wxTE_MULTILINE
)
662 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
666 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
670 void wxTextCtrl::SetEditable( bool editable
)
672 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
674 if (m_windowStyle
& wxTE_MULTILINE
)
676 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
680 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
684 bool wxTextCtrl::Enable( bool enable
)
686 if (!wxWindowBase::Enable(enable
))
692 if (m_windowStyle
& wxTE_MULTILINE
)
694 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
695 OnParentEnable(enable
);
699 gtk_widget_set_sensitive( m_text
, enable
);
705 // wxGTK-specific: called recursively by Enable,
706 // to give widgets an oppprtunity to correct their colours after they
707 // have been changed by Enable
708 void wxTextCtrl::OnParentEnable( bool enable
)
710 // If we have a custom background colour, we use this colour in both
711 // disabled and enabled mode, or we end up with a different colour under the
713 wxColour oldColour
= GetBackgroundColour();
716 // Need to set twice or it'll optimize the useful stuff out
717 if (oldColour
== * wxWHITE
)
718 SetBackgroundColour(*wxBLACK
);
720 SetBackgroundColour(*wxWHITE
);
721 SetBackgroundColour(oldColour
);
725 void wxTextCtrl::MarkDirty()
730 void wxTextCtrl::DiscardEdits()
735 // ----------------------------------------------------------------------------
736 // max text length support
737 // ----------------------------------------------------------------------------
739 void wxTextCtrl::IgnoreNextTextUpdate()
741 m_ignoreNextUpdate
= true;
744 bool wxTextCtrl::IgnoreTextUpdate()
746 if ( m_ignoreNextUpdate
)
748 m_ignoreNextUpdate
= false;
756 void wxTextCtrl::SetMaxLength(unsigned long len
)
758 if ( !HasFlag(wxTE_MULTILINE
) )
760 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
762 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
763 // we had tried to enter more text than allowed by max text length and
764 // the text wasn't really changed
766 // to detect this and generate TEXT_MAXLEN event instead of
767 // TEXT_CHANGED one in this case we also catch "insert_text" signal
769 // when max len is set to 0 we disconnect our handler as it means that
770 // we shouldn't check anything any more
773 gtk_signal_connect( GTK_OBJECT(m_text
),
775 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
780 gtk_signal_disconnect_by_func
783 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
790 void wxTextCtrl::SetSelection( long from
, long to
)
792 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
794 if (from
== -1 && to
== -1)
797 to
= GetValue().Length();
800 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
801 !GTK_TEXT(m_text
)->line_start_cache
)
803 // tell the programmer that it didn't work
804 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
808 if (m_windowStyle
& wxTE_MULTILINE
)
810 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
814 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
818 void wxTextCtrl::ShowPosition( long pos
)
820 if (m_windowStyle
& wxTE_MULTILINE
)
822 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
823 float totalLines
= (float) GetNumberOfLines();
826 PositionToXY(pos
, &posX
, &posY
);
827 float posLine
= (float) posY
;
828 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
829 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
833 long wxTextCtrl::GetInsertionPoint() const
835 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
836 return (long) GET_EDITABLE_POS(m_text
);
839 wxTextPos
wxTextCtrl::GetLastPosition() const
841 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
845 if (m_windowStyle
& wxTE_MULTILINE
)
847 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
851 pos
= GTK_ENTRY(m_text
)->text_length
;
857 void wxTextCtrl::Remove( long from
, long to
)
859 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
860 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
863 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
865 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
871 gint pos
= (gint
)from
;
873 wxWX2MBbuf buf
= value
.mbc_str();
874 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
876 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
877 #endif // wxUSE_UNICODE
881 void wxTextCtrl::Cut()
883 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
884 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
887 void wxTextCtrl::Copy()
889 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
890 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
893 void wxTextCtrl::Paste()
895 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
896 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
900 void wxTextCtrl::Undo()
903 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
906 void wxTextCtrl::Redo()
909 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
912 bool wxTextCtrl::CanUndo() const
915 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
919 bool wxTextCtrl::CanRedo() const
922 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
926 // If the return values from and to are the same, there is no
928 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
930 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
934 bool haveSelection
= false;
936 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
938 haveSelection
= true;
939 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
940 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
943 if (! haveSelection
)
944 from
= to
= GetInsertionPoint();
948 // exchange them to be compatible with wxMSW
961 bool wxTextCtrl::IsEditable() const
963 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
964 return GTK_EDITABLE(m_text
)->editable
;
967 bool wxTextCtrl::IsModified() const
972 void wxTextCtrl::Clear()
974 SetValue( wxEmptyString
);
977 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
979 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
981 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
983 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
984 event
.SetEventObject(this);
985 event
.SetString(GetValue());
986 if (GetEventHandler()->ProcessEvent(event
)) return;
989 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
991 // This will invoke the dialog default action, such
992 // as the clicking the default button.
994 wxWindow
*top_frame
= m_parent
;
995 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
996 top_frame
= top_frame
->GetParent();
998 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1000 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1002 if (window
->default_widget
)
1004 gtk_widget_activate (window
->default_widget
);
1013 GtkWidget
* wxTextCtrl::GetConnectWidget()
1015 return GTK_WIDGET(m_text
);
1018 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1020 if (m_windowStyle
& wxTE_MULTILINE
)
1022 return (window
== GTK_TEXT(m_text
)->text_area
);
1026 return (window
== GTK_ENTRY(m_text
)->text_area
);
1030 // the font will change for subsequent text insertiongs
1031 bool wxTextCtrl::SetFont( const wxFont
&font
)
1033 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1035 if ( !wxTextCtrlBase::SetFont(font
) )
1037 // font didn't change, nothing to do
1041 if ( m_windowStyle
& wxTE_MULTILINE
)
1043 SetUpdateFont(true);
1045 m_defaultStyle
.SetFont(font
);
1047 ChangeFontGlobally();
1053 void wxTextCtrl::ChangeFontGlobally()
1055 // this method is very inefficient and hence should be called as rarely as
1057 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1059 _T("shouldn't be called for single line controls") );
1061 wxString value
= GetValue();
1062 if ( !value
.empty() )
1064 SetUpdateFont(false);
1071 void wxTextCtrl::UpdateFontIfNeeded()
1074 ChangeFontGlobally();
1077 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1079 if ( !wxControl::SetForegroundColour(colour
) )
1082 // update default fg colour too
1083 m_defaultStyle
.SetTextColour(colour
);
1088 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1090 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1092 if ( !wxControl::SetBackgroundColour( colour
) )
1095 if (!m_widget
->window
)
1098 if (!m_backgroundColour
.Ok())
1101 if (m_windowStyle
& wxTE_MULTILINE
)
1103 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1106 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1107 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1108 gdk_window_clear( window
);
1111 // change active background color too
1112 m_defaultStyle
.SetBackgroundColour( colour
);
1117 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1119 if ( m_windowStyle
& wxTE_MULTILINE
)
1121 if ( style
.IsDefault() )
1127 // VERY dirty way to do that - removes the required text and re-adds it
1128 // with styling (FIXME)
1130 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1132 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1133 _T("invalid range in wxTextCtrl::SetStyle") );
1135 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1136 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1137 wxString
tmp(text
,*wxConvCurrent
);
1140 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1141 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1144 wxWX2MBbuf buf
= tmp
.mbc_str();
1145 const char *txt
= buf
;
1146 size_t txtlen
= strlen(buf
);
1148 const char *txt
= tmp
;
1149 size_t txtlen
= tmp
.length();
1152 // use the attributes from style which are set in it and fall back
1153 // first to the default style and then to the text control default
1154 // colours for the others
1155 wxGtkTextInsert(m_text
,
1156 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1160 /* does not seem to help under GTK+ 1.2 !!!
1161 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1162 SetInsertionPoint( old_pos
);
1168 // cannot do this for GTK+'s Entry widget
1172 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1174 gtk_widget_modify_style(m_text
, style
);
1177 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1182 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1187 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1192 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1197 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1202 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1204 event
.Enable( CanCut() );
1207 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1209 event
.Enable( CanCopy() );
1212 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1214 event
.Enable( CanPaste() );
1217 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1219 event
.Enable( CanUndo() );
1222 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1224 event
.Enable( CanRedo() );
1227 void wxTextCtrl::OnInternalIdle()
1229 wxCursor cursor
= m_cursor
;
1230 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1234 GdkWindow
*window
= (GdkWindow
*) NULL
;
1235 if (HasFlag(wxTE_MULTILINE
))
1236 window
= GTK_TEXT(m_text
)->text_area
;
1238 window
= GTK_ENTRY(m_text
)->text_area
;
1241 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1243 if (!g_globalCursor
.Ok())
1244 cursor
= *wxSTANDARD_CURSOR
;
1246 window
= m_widget
->window
;
1247 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1248 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1251 if (g_delayedFocus
== this)
1253 if (GTK_WIDGET_REALIZED(m_widget
))
1255 gtk_widget_grab_focus( m_widget
);
1256 g_delayedFocus
= NULL
;
1260 if (wxUpdateUIEvent::CanUpdate(this))
1261 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1264 wxSize
wxTextCtrl::DoGetBestSize() const
1266 // FIXME should be different for multi-line controls...
1267 wxSize
ret( wxControl::DoGetBestSize() );
1268 wxSize
best(80, ret
.y
);
1269 CacheBestSize(best
);
1273 // ----------------------------------------------------------------------------
1275 // ----------------------------------------------------------------------------
1277 void wxTextCtrl::Freeze()
1279 if ( HasFlag(wxTE_MULTILINE
) )
1281 gtk_text_freeze(GTK_TEXT(m_text
));
1285 void wxTextCtrl::Thaw()
1287 if ( HasFlag(wxTE_MULTILINE
) )
1289 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1291 gtk_text_thaw(GTK_TEXT(m_text
));
1295 // ----------------------------------------------------------------------------
1297 // ----------------------------------------------------------------------------
1299 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1301 if ( !IsMultiLine() )
1304 return GTK_TEXT(m_text
)->vadj
;
1307 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1309 float value
= adj
->value
+ diff
;
1314 float upper
= adj
->upper
- adj
->page_size
;
1315 if ( value
> upper
)
1318 // did we noticeably change the scroll position?
1319 if ( fabs(adj
->value
- value
) < 0.2 )
1321 // well, this is what Robert does in wxScrollBar, so it must be good...
1326 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1331 bool wxTextCtrl::ScrollLines(int lines
)
1333 GtkAdjustment
*adj
= GetVAdj();
1337 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1338 int diff
= 10*lines
;
1340 return DoScroll(adj
, diff
);
1343 bool wxTextCtrl::ScrollPages(int pages
)
1345 GtkAdjustment
*adj
= GetVAdj();
1349 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
1355 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1357 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);