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"
22 #include "wx/settings.h"
24 #include "wx/strconv.h"
25 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
27 #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
,
59 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
62 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
65 GdkColor
*colBg
= attr
.HasBackgroundColour()
66 ? attr
.GetBackgroundColour().GetColor()
69 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
73 // ----------------------------------------------------------------------------
74 // "insert_text" for GtkEntry
75 // ----------------------------------------------------------------------------
79 gtk_insert_text_callback(GtkEditable
*editable
,
80 const gchar
*new_text
,
86 wxapp_install_idle_handler();
88 // we should only be called if we have a max len limit at all
89 GtkEntry
*entry
= GTK_ENTRY (editable
);
91 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
93 // check that we don't overflow the max length limit
95 // FIXME: this doesn't work when we paste a string which is going to be
97 if ( entry
->text_length
== entry
->text_max_length
)
99 // we don't need to run the base class version at all
100 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
102 // remember that the next changed signal is to be ignored to avoid
103 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
104 win
->IgnoreNextTextUpdate();
106 // and generate the correct one ourselves
107 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
108 event
.SetEventObject(win
);
109 event
.SetString(win
->GetValue());
110 win
->GetEventHandler()->ProcessEvent( event
);
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
121 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
123 if ( win
->IgnoreTextUpdate() )
126 if (!win
->m_hasVMT
) return;
129 wxapp_install_idle_handler();
132 win
->UpdateFontIfNeeded();
134 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
135 event
.SetEventObject( win
);
136 win
->GetEventHandler()->ProcessEvent( event
);
140 //-----------------------------------------------------------------------------
141 // "changed" from vertical scrollbar
142 //-----------------------------------------------------------------------------
146 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
148 if (!win
->m_hasVMT
) return;
151 wxapp_install_idle_handler();
153 win
->CalculateScrollbar();
157 // ----------------------------------------------------------------------------
158 // redraw callback for multiline text
159 // ----------------------------------------------------------------------------
161 // redrawing a GtkText from inside a wxYield() call results in crashes (the
162 // text sample shows it in its "Add lines" command which shows wxProgressDialog
163 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
164 // don't do anything if we're inside wxYield()
166 extern bool wxIsInsideYield
;
169 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
172 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
175 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
177 if ( !wxIsInsideYield
)
179 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
180 _T("infinite recursion in wxgtk_text_draw aborted") );
182 gs_gtk_text_draw(widget
, rect
);
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
193 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
194 EVT_CHAR(wxTextCtrl::OnChar
)
196 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
197 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
198 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
199 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
200 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
202 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
203 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
204 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
205 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
206 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
209 void wxTextCtrl::Init()
213 SetUpdateFont(false);
215 m_vScrollbar
= (GtkWidget
*)NULL
;
218 wxTextCtrl::~wxTextCtrl()
222 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
224 const wxString
&value
,
228 const wxValidator
& validator
,
229 const wxString
&name
)
233 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
236 bool wxTextCtrl::Create( wxWindow
*parent
,
238 const wxString
&value
,
242 const wxValidator
& validator
,
243 const wxString
&name
)
246 m_acceptsFocus
= true;
248 if (!PreCreation( parent
, pos
, size
) ||
249 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
251 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
256 m_vScrollbarVisible
= false;
258 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
262 // create our control ...
263 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
265 // ... and put into the upper left hand corner of the table
266 bool bHasHScrollbar
= false;
267 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
268 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
269 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
270 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
271 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
275 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
277 // finally, put the vertical scrollbar in the upper right corner
278 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
279 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
280 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
282 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
287 // a single-line text control: no need for scrollbars
289 m_text
= gtk_entry_new();
292 m_parent
->DoAddChild( this );
294 m_focusWidget
= m_text
;
299 gtk_widget_show(m_text
);
303 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
304 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
306 // only initialize gs_gtk_text_draw once, starting from the next the
307 // klass::draw will already be wxgtk_text_draw
308 if ( !gs_gtk_text_draw
)
311 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
313 gs_gtk_text_draw
= draw
;
315 draw
= wxgtk_text_draw
;
321 #if !GTK_CHECK_VERSION(1, 2, 0)
322 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
323 // gtk_editable_insert_text()
324 gtk_widget_realize(m_text
);
329 wxWX2MBbuf val
= value
.mbc_str();
330 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
332 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &tmp
);
337 // Bring editable's cursor uptodate. Bug in GTK.
338 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
342 if (style
& wxTE_PASSWORD
)
345 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
348 if (style
& wxTE_READONLY
)
351 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
356 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
359 // We want to be notified about text changes.
360 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
361 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
363 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
365 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
366 SetDefaultStyle( attrDef
);
372 void wxTextCtrl::CalculateScrollbar()
374 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
376 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
378 if (adj
->upper
- adj
->page_size
< 0.8)
380 if (m_vScrollbarVisible
)
382 gtk_widget_hide( m_vScrollbar
);
383 m_vScrollbarVisible
= false;
388 if (!m_vScrollbarVisible
)
390 gtk_widget_show( m_vScrollbar
);
391 m_vScrollbarVisible
= true;
396 wxString
wxTextCtrl::GetValue() const
398 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
401 if (m_windowStyle
& wxTE_MULTILINE
)
403 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
404 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
410 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
416 void wxTextCtrl::SetValue( const wxString
&value
)
418 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
420 if (m_windowStyle
& wxTE_MULTILINE
)
422 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
423 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
425 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.length(), &len
);
429 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
432 // GRG, Jun/2000: Changed this after a lot of discussion in
433 // the lists. wxWidgets 2.2 will have a set of flags to
434 // customize this behaviour.
435 SetInsertionPoint(0);
440 void wxTextCtrl::WriteText( const wxString
&text
)
442 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
447 // gtk_text_changed_callback() will set m_modified to true but m_modified
448 // shouldn't be changed by the program writing to the text control itself,
449 // so save the old value and restore when we're done
450 bool oldModified
= m_modified
;
452 if ( m_windowStyle
& wxTE_MULTILINE
)
454 // After cursor movements, gtk_text_get_point() is wrong by one.
455 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
457 // always use m_defaultStyle, even if it is empty as otherwise
458 // resetting the style and appending some more text wouldn't work: if
459 // we don't specify the style explicitly, the old style would be used
460 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
461 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.Len());
463 // we called wxGtkTextInsert with correct font, no need to do anything
464 // in UpdateFontIfNeeded() any longer
467 SetUpdateFont(false);
470 // Bring editable's cursor back uptodate.
471 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
475 // First remove the selection if there is one
476 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
478 // This moves the cursor pos to behind the inserted text.
479 gint len
= GET_EDITABLE_POS(m_text
);
481 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.Len(), &len
);
483 // Bring entry's cursor uptodate.
484 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
487 m_modified
= oldModified
;
490 void wxTextCtrl::AppendText( const wxString
&text
)
492 SetInsertionPointEnd();
496 wxString
wxTextCtrl::GetLineText( long lineNo
) const
498 if (m_windowStyle
& wxTE_MULTILINE
)
500 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
501 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
508 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
513 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
521 return wxEmptyString
;
526 if (lineNo
== 0) return GetValue();
527 return wxEmptyString
;
531 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
533 /* If you implement this, don't forget to update the documentation!
534 * (file docs/latex/wx/text.tex) */
535 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
538 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
540 if ( m_windowStyle
& wxTE_MULTILINE
)
542 wxString text
= GetValue();
544 // cast to prevent warning. But pos really should've been unsigned.
545 if( (unsigned long)pos
> text
.Len() )
551 const wxChar
* stop
= text
.c_str() + pos
;
552 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
563 else // single line control
565 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
572 // index out of bounds
580 long wxTextCtrl::XYToPosition(long x
, long y
) const
582 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
585 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
591 int wxTextCtrl::GetLineLength(long lineNo
) const
593 wxString str
= GetLineText (lineNo
);
594 return (int) str
.length();
597 int wxTextCtrl::GetNumberOfLines() const
599 if (m_windowStyle
& wxTE_MULTILINE
)
601 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
602 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
607 for (int i
= 0; i
< len
; i
++ )
614 // currentLine is 0 based, add 1 to get number of lines
615 return currentLine
+ 1;
628 void wxTextCtrl::SetInsertionPoint( long pos
)
630 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
634 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
635 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
637 /* we fake a set_point by inserting and deleting. as the user
638 isn't supposed to get to know about this non-sense, we
639 disconnect so that no events are sent to the user program. */
641 gint tmp
= (gint
)pos
;
642 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
643 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
645 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
646 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
648 // bring editable's cursor uptodate. Bug in GTK.
649 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
653 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
655 // Bring editable's cursor uptodate. Bug in GTK.
656 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
660 void wxTextCtrl::SetInsertionPointEnd()
662 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
664 if (m_windowStyle
& wxTE_MULTILINE
)
666 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
670 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
674 void wxTextCtrl::SetEditable( bool editable
)
676 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
678 if (m_windowStyle
& wxTE_MULTILINE
)
680 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
684 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
688 bool wxTextCtrl::Enable( bool enable
)
690 if (!wxWindowBase::Enable(enable
))
696 if (m_windowStyle
& wxTE_MULTILINE
)
698 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
699 OnParentEnable(enable
);
703 gtk_widget_set_sensitive( m_text
, enable
);
709 // wxGTK-specific: called recursively by Enable,
710 // to give widgets an oppprtunity to correct their colours after they
711 // have been changed by Enable
712 void wxTextCtrl::OnParentEnable( bool enable
)
714 // If we have a custom background colour, we use this colour in both
715 // disabled and enabled mode, or we end up with a different colour under the
717 wxColour oldColour
= GetBackgroundColour();
720 // Need to set twice or it'll optimize the useful stuff out
721 if (oldColour
== * wxWHITE
)
722 SetBackgroundColour(*wxBLACK
);
724 SetBackgroundColour(*wxWHITE
);
725 SetBackgroundColour(oldColour
);
729 void wxTextCtrl::MarkDirty()
734 void wxTextCtrl::DiscardEdits()
739 // ----------------------------------------------------------------------------
740 // max text length support
741 // ----------------------------------------------------------------------------
743 void wxTextCtrl::IgnoreNextTextUpdate()
745 m_ignoreNextUpdate
= true;
748 bool wxTextCtrl::IgnoreTextUpdate()
750 if ( m_ignoreNextUpdate
)
752 m_ignoreNextUpdate
= false;
760 void wxTextCtrl::SetMaxLength(unsigned long len
)
762 if ( !HasFlag(wxTE_MULTILINE
) )
764 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
766 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
767 // we had tried to enter more text than allowed by max text length and
768 // the text wasn't really changed
770 // to detect this and generate TEXT_MAXLEN event instead of
771 // TEXT_CHANGED one in this case we also catch "insert_text" signal
773 // when max len is set to 0 we disconnect our handler as it means that
774 // we shouldn't check anything any more
777 gtk_signal_connect( GTK_OBJECT(m_text
),
779 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
784 gtk_signal_disconnect_by_func
787 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
794 void wxTextCtrl::SetSelection( long from
, long to
)
796 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
798 if (from
== -1 && to
== -1)
801 to
= GetValue().length();
804 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
805 !GTK_TEXT(m_text
)->line_start_cache
)
807 // tell the programmer that it didn't work
808 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
812 if (m_windowStyle
& wxTE_MULTILINE
)
814 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
818 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
822 void wxTextCtrl::ShowPosition( long pos
)
824 if (m_windowStyle
& wxTE_MULTILINE
)
826 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
827 float totalLines
= (float) GetNumberOfLines();
830 PositionToXY(pos
, &posX
, &posY
);
831 float posLine
= (float) posY
;
832 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
833 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
837 long wxTextCtrl::GetInsertionPoint() const
839 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
840 return (long) GET_EDITABLE_POS(m_text
);
843 wxTextPos
wxTextCtrl::GetLastPosition() const
845 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
849 if (m_windowStyle
& wxTE_MULTILINE
)
851 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
855 pos
= GTK_ENTRY(m_text
)->text_length
;
861 void wxTextCtrl::Remove( long from
, long to
)
863 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
864 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
867 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
869 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
875 gint pos
= (gint
)from
;
877 wxWX2MBbuf buf
= value
.mbc_str();
878 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
880 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &pos
);
881 #endif // wxUSE_UNICODE
885 void wxTextCtrl::Cut()
887 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
888 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
891 void wxTextCtrl::Copy()
893 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
894 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
897 void wxTextCtrl::Paste()
899 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
900 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
904 void wxTextCtrl::Undo()
907 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
910 void wxTextCtrl::Redo()
913 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
916 bool wxTextCtrl::CanUndo() const
919 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
923 bool wxTextCtrl::CanRedo() const
926 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
930 // If the return values from and to are the same, there is no
932 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
934 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
938 bool haveSelection
= false;
940 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
942 haveSelection
= true;
943 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
944 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
947 if (! haveSelection
)
948 from
= to
= GetInsertionPoint();
952 // exchange them to be compatible with wxMSW
965 bool wxTextCtrl::IsEditable() const
967 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
968 return GTK_EDITABLE(m_text
)->editable
;
971 bool wxTextCtrl::IsModified() const
976 void wxTextCtrl::Clear()
978 SetValue( wxEmptyString
);
981 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
983 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
985 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
987 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
988 event
.SetEventObject(this);
989 event
.SetString(GetValue());
990 if (GetEventHandler()->ProcessEvent(event
)) return;
993 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
995 // This will invoke the dialog default action, such
996 // as the clicking the default button.
998 wxWindow
*top_frame
= m_parent
;
999 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1000 top_frame
= top_frame
->GetParent();
1002 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1004 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1006 if (window
->default_widget
)
1008 gtk_widget_activate (window
->default_widget
);
1017 GtkWidget
* wxTextCtrl::GetConnectWidget()
1019 return GTK_WIDGET(m_text
);
1022 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1024 if (m_windowStyle
& wxTE_MULTILINE
)
1026 return (window
== GTK_TEXT(m_text
)->text_area
);
1030 return (window
== GTK_ENTRY(m_text
)->text_area
);
1034 // the font will change for subsequent text insertiongs
1035 bool wxTextCtrl::SetFont( const wxFont
&font
)
1037 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1039 if ( !wxTextCtrlBase::SetFont(font
) )
1041 // font didn't change, nothing to do
1045 if ( m_windowStyle
& wxTE_MULTILINE
)
1047 SetUpdateFont(true);
1049 m_defaultStyle
.SetFont(font
);
1051 ChangeFontGlobally();
1057 void wxTextCtrl::ChangeFontGlobally()
1059 // this method is very inefficient and hence should be called as rarely as
1061 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1063 _T("shouldn't be called for single line controls") );
1065 wxString value
= GetValue();
1066 if ( !value
.empty() )
1068 SetUpdateFont(false);
1075 void wxTextCtrl::UpdateFontIfNeeded()
1078 ChangeFontGlobally();
1081 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1083 if ( !wxControl::SetForegroundColour(colour
) )
1086 // update default fg colour too
1087 m_defaultStyle
.SetTextColour(colour
);
1092 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1094 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1096 if ( !wxControl::SetBackgroundColour( colour
) )
1099 if (!m_widget
->window
)
1102 if (!m_backgroundColour
.Ok())
1105 if (m_windowStyle
& wxTE_MULTILINE
)
1107 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1110 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1111 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1112 gdk_window_clear( window
);
1115 // change active background color too
1116 m_defaultStyle
.SetBackgroundColour( colour
);
1121 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1123 if ( m_windowStyle
& wxTE_MULTILINE
)
1125 if ( style
.IsDefault() )
1131 // VERY dirty way to do that - removes the required text and re-adds it
1132 // with styling (FIXME)
1134 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1136 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1137 _T("invalid range in wxTextCtrl::SetStyle") );
1139 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1140 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1141 wxString
tmp(text
,*wxConvCurrent
);
1144 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1145 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1148 wxWX2MBbuf buf
= tmp
.mbc_str();
1149 const char *txt
= buf
;
1150 size_t txtlen
= strlen(buf
);
1152 const char *txt
= tmp
;
1153 size_t txtlen
= tmp
.length();
1156 // use the attributes from style which are set in it and fall back
1157 // first to the default style and then to the text control default
1158 // colours for the others
1159 wxGtkTextInsert(m_text
,
1160 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1164 /* does not seem to help under GTK+ 1.2 !!!
1165 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1166 SetInsertionPoint( old_pos
);
1172 // cannot do this for GTK+'s Entry widget
1176 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1178 gtk_widget_modify_style(m_text
, style
);
1181 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1186 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1191 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1196 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1201 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1206 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1208 event
.Enable( CanCut() );
1211 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1213 event
.Enable( CanCopy() );
1216 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1218 event
.Enable( CanPaste() );
1221 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1223 event
.Enable( CanUndo() );
1226 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1228 event
.Enable( CanRedo() );
1231 void wxTextCtrl::OnInternalIdle()
1233 wxCursor cursor
= m_cursor
;
1234 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1238 GdkWindow
*window
= (GdkWindow
*) NULL
;
1239 if (HasFlag(wxTE_MULTILINE
))
1240 window
= GTK_TEXT(m_text
)->text_area
;
1242 window
= GTK_ENTRY(m_text
)->text_area
;
1245 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1247 if (!g_globalCursor
.Ok())
1248 cursor
= *wxSTANDARD_CURSOR
;
1250 window
= m_widget
->window
;
1251 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1252 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1255 if (g_delayedFocus
== this)
1257 if (GTK_WIDGET_REALIZED(m_widget
))
1259 gtk_widget_grab_focus( m_widget
);
1260 g_delayedFocus
= NULL
;
1264 if (wxUpdateUIEvent::CanUpdate(this))
1265 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1268 wxSize
wxTextCtrl::DoGetBestSize() const
1270 // FIXME should be different for multi-line controls...
1271 wxSize
ret( wxControl::DoGetBestSize() );
1272 wxSize
best(80, ret
.y
);
1273 CacheBestSize(best
);
1277 // ----------------------------------------------------------------------------
1279 // ----------------------------------------------------------------------------
1281 void wxTextCtrl::Freeze()
1283 if ( HasFlag(wxTE_MULTILINE
) )
1285 gtk_text_freeze(GTK_TEXT(m_text
));
1289 void wxTextCtrl::Thaw()
1291 if ( HasFlag(wxTE_MULTILINE
) )
1293 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1295 gtk_text_thaw(GTK_TEXT(m_text
));
1299 // ----------------------------------------------------------------------------
1301 // ----------------------------------------------------------------------------
1303 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1305 if ( !IsMultiLine() )
1308 return GTK_TEXT(m_text
)->vadj
;
1311 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1313 float value
= adj
->value
+ diff
;
1318 float upper
= adj
->upper
- adj
->page_size
;
1319 if ( value
> upper
)
1322 // did we noticeably change the scroll position?
1323 if ( fabs(adj
->value
- value
) < 0.2 )
1325 // well, this is what Robert does in wxScrollBar, so it must be good...
1330 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1335 bool wxTextCtrl::ScrollLines(int lines
)
1337 GtkAdjustment
*adj
= GetVAdj();
1341 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1342 int diff
= 10*lines
;
1344 return DoScroll(adj
, diff
);
1347 bool wxTextCtrl::ScrollPages(int pages
)
1349 GtkAdjustment
*adj
= GetVAdj();
1353 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
1359 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1361 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);