1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "textctrl.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/textctrl.h"
21 #include "wx/settings.h"
23 #include "wx/strconv.h"
24 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
26 #include <sys/types.h>
29 #include <math.h> // for fabs
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern void wxapp_install_idle_handler();
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern bool g_blockEventsOnDrag
;
46 extern wxCursor g_globalCursor
;
47 extern wxWindowGTK
*g_delayedFocus
;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
54 static void wxGtkTextInsert(GtkWidget
*text
,
55 GtkTextBuffer
*text_buffer
,
56 const wxTextAttr
& attr
,
60 PangoFontDescription
*font_description
= attr
.HasFont()
61 ? attr
.GetFont().GetNativeFontInfo()->description
64 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
67 GdkColor
*colBg
= attr
.HasBackgroundColour()
68 ? attr
.GetBackgroundColour().GetColor()
71 GtkTextIter start
, end
;
73 // iterators are invalidated by any mutation that affects 'indexable' buffer contents,
74 // so we save current position in a mark
75 // we need a mark of left gravity, so we cannot use
76 // mark = gtk_text_buffer_get_insert (text_buffer)
78 gtk_text_buffer_get_iter_at_mark( text_buffer
, &start
,
79 gtk_text_buffer_get_insert (text_buffer
) );
80 mark
= gtk_text_buffer_create_mark( text_buffer
, NULL
, &start
, TRUE
/*left gravity*/ );
82 gtk_text_buffer_insert_at_cursor( text_buffer
, buffer
, strlen(buffer
) );
84 gtk_text_buffer_get_iter_at_mark( text_buffer
, &end
,
85 gtk_text_buffer_get_insert (text_buffer
) );
86 gtk_text_buffer_get_iter_at_mark( text_buffer
, &start
, mark
);
89 tag
= gtk_text_buffer_create_tag( text_buffer
, NULL
, "font-desc", font_description
,
90 "foreground-gdk", colFg
,
91 "background-gdk", colBg
, NULL
);
92 gtk_text_buffer_apply_tag( text_buffer
, tag
, &start
, &end
);
95 static void wxGtkTextInsert(GtkWidget
*text
,
96 const wxTextAttr
& attr
,
100 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
103 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
106 GdkColor
*colBg
= attr
.HasBackgroundColour()
107 ? attr
.GetBackgroundColour().GetColor()
110 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
114 // ----------------------------------------------------------------------------
115 // "insert_text" for GtkEntry
116 // ----------------------------------------------------------------------------
119 gtk_insert_text_callback(GtkEditable
*editable
,
120 const gchar
*new_text
,
121 gint new_text_length
,
126 wxapp_install_idle_handler();
128 // we should only be called if we have a max len limit at all
129 GtkEntry
*entry
= GTK_ENTRY (editable
);
131 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
133 // check that we don't overflow the max length limit
135 // FIXME: this doesn't work when we paste a string which is going to be
137 if ( entry
->text_length
== entry
->text_max_length
)
139 // we don't need to run the base class version at all
140 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
142 // remember that the next changed signal is to be ignored to avoid
143 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
144 win
->IgnoreNextTextUpdate();
146 // and generate the correct one ourselves
147 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
148 event
.SetEventObject(win
);
149 event
.SetString(win
->GetValue());
150 win
->GetEventHandler()->ProcessEvent( event
);
154 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
159 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
161 if ( win
->IgnoreTextUpdate() )
164 if (!win
->m_hasVMT
) return;
167 wxapp_install_idle_handler();
170 win
->UpdateFontIfNeeded();
172 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
173 event
.SetEventObject( win
);
174 event
.SetString( win
->GetValue() );
175 win
->GetEventHandler()->ProcessEvent( event
);
178 //-----------------------------------------------------------------------------
179 // "changed" from vertical scrollbar
180 //-----------------------------------------------------------------------------
184 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
186 if (!win
->m_hasVMT
) return;
189 wxapp_install_idle_handler();
191 win
->CalculateScrollbar();
195 // ----------------------------------------------------------------------------
196 // redraw callback for multiline text
197 // ----------------------------------------------------------------------------
201 // redrawing a GtkText from inside a wxYield() call results in crashes (the
202 // text sample shows it in its "Add lines" command which shows wxProgressDialog
203 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
204 // don't do anything if we're inside wxYield()
206 extern bool wxIsInsideYield
;
209 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
212 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
215 void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
217 if ( !wxIsInsideYield
)
219 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
220 _T("infinite recursion in wxgtk_text_draw aborted") );
222 gs_gtk_text_draw(widget
, rect
);
226 #endif // __WXGTK20__
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
232 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
234 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
235 EVT_CHAR(wxTextCtrl::OnChar
)
237 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
238 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
239 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
240 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
241 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
243 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
244 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
245 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
246 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
247 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
250 void wxTextCtrl::Init()
254 m_updateFont
= FALSE
;
256 m_vScrollbar
= (GtkWidget
*)NULL
;
259 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
261 const wxString
&value
,
265 const wxValidator
& validator
,
266 const wxString
&name
)
270 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
273 bool wxTextCtrl::Create( wxWindow
*parent
,
275 const wxString
&value
,
279 const wxValidator
& validator
,
280 const wxString
&name
)
283 m_acceptsFocus
= TRUE
;
285 if (!PreCreation( parent
, pos
, size
) ||
286 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
288 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
293 m_vScrollbarVisible
= FALSE
;
295 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
298 GtkTextBuffer
*buffer
= NULL
;
305 m_text
= gtk_text_view_new();
307 buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
309 // create scrolled window
310 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
311 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
312 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
314 // Insert view into scrolled window
315 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
317 // Global settings which can be overridden by tags, I guess.
318 if (HasFlag( wxHSCROLL
))
319 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), GTK_WRAP_NONE
);
321 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), GTK_WRAP_WORD
);
323 if (!HasFlag(wxNO_BORDER
))
324 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget
), GTK_SHADOW_IN
);
326 // create our control ...
327 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
329 // ... and put into the upper left hand corner of the table
330 bool bHasHScrollbar
= FALSE
;
331 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
332 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
333 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
334 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
335 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
339 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
341 // finally, put the vertical scrollbar in the upper right corner
342 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
343 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
344 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
346 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
352 // a single-line text control: no need for scrollbars
354 m_text
= gtk_entry_new();
357 m_parent
->DoAddChild( this );
359 m_focusWidget
= m_text
;
363 SetFont( parent
->GetFont() );
365 wxSize
size_best( DoGetBestSize() );
366 wxSize
new_size( size
);
367 if (new_size
.x
== -1)
368 new_size
.x
= size_best
.x
;
369 if (new_size
.y
== -1)
370 new_size
.y
= size_best
.y
;
371 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
372 SetSize( new_size
.x
, new_size
.y
);
375 gtk_widget_show(m_text
);
380 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
381 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
383 // only initialize gs_gtk_text_draw once, starting from the next the
384 // klass::draw will already be wxgtk_text_draw
385 if ( !gs_gtk_text_draw
)
388 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
390 gs_gtk_text_draw
= draw
;
392 draw
= wxgtk_text_draw
;
397 if (!value
.IsEmpty())
403 #if !GTK_CHECK_VERSION(1, 2, 0)
404 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
405 // gtk_editable_insert_text()
406 gtk_widget_realize(m_text
);
411 wxWX2MBbuf val
= value
.mbc_str();
412 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
414 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
419 // Bring editable's cursor uptodate. Bug in GTK.
420 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
426 if (style
& wxTE_PASSWORD
)
429 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
432 if (style
& wxTE_READONLY
)
435 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
438 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text
), FALSE
);
445 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
449 // We want to be notified about text changes.
453 g_signal_connect( G_OBJECT(buffer
), "changed",
454 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
459 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
460 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
463 // we don't set a valid background colour, because the window
464 // manager should use a default one
465 m_backgroundColour
= wxColour();
467 wxColour colFg
= parent
->GetForegroundColour();
468 SetForegroundColour( colFg
);
470 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
472 wxTextAttr
attrDef( colFg
, m_backgroundColour
, parent
->GetFont() );
473 SetDefaultStyle( attrDef
);
480 void wxTextCtrl::CalculateScrollbar()
483 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
485 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
487 if (adj
->upper
- adj
->page_size
< 0.8)
489 if (m_vScrollbarVisible
)
491 gtk_widget_hide( m_vScrollbar
);
492 m_vScrollbarVisible
= FALSE
;
497 if (!m_vScrollbarVisible
)
499 gtk_widget_show( m_vScrollbar
);
500 m_vScrollbarVisible
= TRUE
;
506 wxString
wxTextCtrl::GetValue() const
508 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
511 if (m_windowStyle
& wxTE_MULTILINE
)
514 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
517 gtk_text_buffer_get_start_iter( text_buffer
, &start
);
519 gtk_text_buffer_get_end_iter( text_buffer
, &end
);
520 gchar
*text
= gtk_text_buffer_get_text( text_buffer
, &start
, &end
, TRUE
);
523 wxWCharBuffer
buffer( wxConvUTF8
.cMB2WX( text
) );
525 wxCharBuffer
buffer( wxConvLocal
.cWC2WX( wxConvUTF8
.cMB2WC( text
) ) );
531 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
532 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
539 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
545 void wxTextCtrl::SetValue( const wxString
&value
)
547 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
549 if (m_windowStyle
& wxTE_MULTILINE
)
554 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( value
) );
556 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( value
) ) );
558 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
559 gtk_text_buffer_set_text( text_buffer
, buffer
, strlen(buffer
) );
562 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
563 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
565 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
570 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
573 // GRG, Jun/2000: Changed this after a lot of discussion in
574 // the lists. wxWindows 2.2 will have a set of flags to
575 // customize this behaviour.
576 SetInsertionPoint(0);
581 void wxTextCtrl::WriteText( const wxString
&text
)
583 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
588 if ( m_windowStyle
& wxTE_MULTILINE
)
593 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( text
) );
595 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) ) );
597 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
599 // TODO: Call whatever is needed to delete the selection.
600 wxGtkTextInsert( m_text
, text_buffer
, m_defaultStyle
, buffer
);
604 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
, gtk_text_buffer_get_insert( text_buffer
) );
605 gtk_text_view_scroll_to_iter( GTK_TEXT_VIEW(m_text
), &iter
, 0.0, FALSE
, 0.0, 1.0 );
607 // After cursor movements, gtk_text_get_point() is wrong by one.
608 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
610 // always use m_defaultStyle, even if it is empty as otherwise
611 // resetting the style and appending some more text wouldn't work: if
612 // we don't specify the style explicitly, the old style would be used
613 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
614 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.Len());
616 // Bring editable's cursor back uptodate.
617 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
618 #endif // GTK 1.x/2.0
622 // First remove the selection if there is one
623 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
625 // This moves the cursor pos to behind the inserted text.
626 gint len
= GET_EDITABLE_POS(m_text
);
631 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( text
) );
633 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) ) );
635 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
638 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.Len(), &len
);
641 // Bring entry's cursor uptodate.
642 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
648 void wxTextCtrl::AppendText( const wxString
&text
)
650 SetInsertionPointEnd();
654 wxString
wxTextCtrl::GetLineText( long lineNo
) const
656 if (m_windowStyle
& wxTE_MULTILINE
)
659 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
660 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
664 wxString
buf(wxT(""));
667 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
672 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
681 return wxEmptyString
;
686 if (lineNo
== 0) return GetValue();
687 return wxEmptyString
;
691 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
693 /* If you implement this, don't forget to update the documentation!
694 * (file docs/latex/wx/text.tex) */
695 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
698 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
700 if ( m_windowStyle
& wxTE_MULTILINE
)
702 wxString text
= GetValue();
704 // cast to prevent warning. But pos really should've been unsigned.
705 if( (unsigned long)pos
> text
.Len() )
711 const wxChar
* stop
= text
.c_str() + pos
;
712 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
723 else // single line control
725 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
732 // index out of bounds
740 long wxTextCtrl::XYToPosition(long x
, long y
) const
742 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
745 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
751 int wxTextCtrl::GetLineLength(long lineNo
) const
753 wxString str
= GetLineText (lineNo
);
754 return (int) str
.Length();
757 int wxTextCtrl::GetNumberOfLines() const
759 if (m_windowStyle
& wxTE_MULTILINE
)
762 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
764 return gtk_text_buffer_get_line_count( buffer
);
766 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
767 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
772 for (int i
= 0; i
< len
; i
++ )
779 // currentLine is 0 based, add 1 to get number of lines
780 return currentLine
+ 1;
794 void wxTextCtrl::SetInsertionPoint( long pos
)
796 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
798 if (m_windowStyle
& wxTE_MULTILINE
)
801 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
803 gtk_text_buffer_get_iter_at_offset( text_buffer
, &iter
, pos
);
804 gtk_text_buffer_place_cursor( text_buffer
, &iter
);
806 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
807 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
809 /* we fake a set_point by inserting and deleting. as the user
810 isn't supposed to get to know about this non-sense, we
811 disconnect so that no events are sent to the user program. */
813 gint tmp
= (gint
)pos
;
814 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
815 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
817 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
818 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
820 // bring editable's cursor uptodate. Bug in GTK.
821 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
826 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
828 // Bring editable's cursor uptodate. Bug in GTK.
829 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
833 void wxTextCtrl::SetInsertionPointEnd()
835 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
837 if (m_windowStyle
& wxTE_MULTILINE
)
840 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
842 gtk_text_buffer_get_end_iter( text_buffer
, &end
);
843 gtk_text_buffer_place_cursor( text_buffer
, &end
);
845 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
850 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
854 void wxTextCtrl::SetEditable( bool editable
)
856 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
858 if (m_windowStyle
& wxTE_MULTILINE
)
861 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
863 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
868 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
872 bool wxTextCtrl::Enable( bool enable
)
874 if (!wxWindowBase::Enable(enable
))
880 if (m_windowStyle
& wxTE_MULTILINE
)
883 SetEditable( enable
);
885 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
886 OnParentEnable(enable
);
891 gtk_widget_set_sensitive( m_text
, enable
);
897 // wxGTK-specific: called recursively by Enable,
898 // to give widgets an oppprtunity to correct their colours after they
899 // have been changed by Enable
900 void wxTextCtrl::OnParentEnable( bool enable
)
902 // If we have a custom background colour, we use this colour in both
903 // disabled and enabled mode, or we end up with a different colour under the
905 wxColour oldColour
= GetBackgroundColour();
908 // Need to set twice or it'll optimize the useful stuff out
909 if (oldColour
== * wxWHITE
)
910 SetBackgroundColour(*wxBLACK
);
912 SetBackgroundColour(*wxWHITE
);
913 SetBackgroundColour(oldColour
);
917 void wxTextCtrl::DiscardEdits()
922 // ----------------------------------------------------------------------------
923 // max text length support
924 // ----------------------------------------------------------------------------
926 void wxTextCtrl::IgnoreNextTextUpdate()
928 m_ignoreNextUpdate
= TRUE
;
931 bool wxTextCtrl::IgnoreTextUpdate()
933 if ( m_ignoreNextUpdate
)
935 m_ignoreNextUpdate
= FALSE
;
943 void wxTextCtrl::SetMaxLength(unsigned long len
)
945 if ( !HasFlag(wxTE_MULTILINE
) )
947 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
949 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
950 // we had tried to enter more text than allowed by max text length and
951 // the text wasn't really changed
953 // to detect this and generate TEXT_MAXLEN event instead of
954 // TEXT_CHANGED one in this case we also catch "insert_text" signal
956 // when max len is set to 0 we disconnect our handler as it means that
957 // we shouldn't check anything any more
960 gtk_signal_connect( GTK_OBJECT(m_text
),
962 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
967 gtk_signal_disconnect_by_func
970 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
977 void wxTextCtrl::SetSelection( long from
, long to
)
979 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
981 if (from
== -1 && to
== -1)
984 to
= GetValue().Length();
988 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
989 !GTK_TEXT(m_text
)->line_start_cache
)
991 // tell the programmer that it didn't work
992 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
997 if (m_windowStyle
& wxTE_MULTILINE
)
1000 GtkTextBuffer
*buf
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
1002 GtkTextIter fromi
, toi
;
1003 gtk_text_buffer_get_iter_at_offset( buf
, &fromi
, from
);
1004 gtk_text_buffer_get_iter_at_offset( buf
, &toi
, to
);
1006 gtk_text_buffer_place_cursor( buf
, &toi
);
1007 gtk_text_buffer_move_mark_by_name( buf
, "selection_bound", &fromi
);
1009 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1014 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1018 void wxTextCtrl::ShowPosition( long pos
)
1021 if (m_windowStyle
& wxTE_MULTILINE
)
1023 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
1024 float totalLines
= (float) GetNumberOfLines();
1027 PositionToXY(pos
, &posX
, &posY
);
1028 float posLine
= (float) posY
;
1029 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
1030 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
1035 long wxTextCtrl::GetInsertionPoint() const
1037 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1040 if (m_windowStyle
& wxTE_MULTILINE
)
1042 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
1044 // There is no direct accessor for the cursor, but
1045 // internally, the cursor is the "mark" called
1046 // "insert" in the text view's btree structure.
1048 GtkTextMark
*mark
= gtk_text_buffer_get_insert( text_buffer
);
1050 gtk_text_buffer_get_iter_at_mark( text_buffer
, &cursor
, mark
);
1052 return gtk_text_iter_get_offset( &cursor
);
1057 return (long) GET_EDITABLE_POS(m_text
);
1061 long wxTextCtrl::GetLastPosition() const
1063 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1067 if (m_windowStyle
& wxTE_MULTILINE
)
1070 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
1072 gtk_text_buffer_get_end_iter( text_buffer
, &end
);
1074 pos
= gtk_text_iter_get_offset( &end
);
1076 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
1081 pos
= GTK_ENTRY(m_text
)->text_length
;
1087 void wxTextCtrl::Remove( long from
, long to
)
1089 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1092 if (m_windowStyle
& wxTE_MULTILINE
)
1095 text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
1097 GtkTextIter fromi
, toi
;
1098 gtk_text_buffer_get_iter_at_offset( text_buffer
, &fromi
, from
);
1099 gtk_text_buffer_get_iter_at_offset( text_buffer
, &toi
, to
);
1101 gtk_text_buffer_delete( text_buffer
, &fromi
, &toi
);
1105 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1108 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1110 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1114 if (!value
.IsEmpty())
1117 SetInsertionPoint( from
);
1120 gint pos
= (gint
)from
;
1122 wxWX2MBbuf buf
= value
.mbc_str();
1123 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
1125 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
1126 #endif // wxUSE_UNICODE
1127 #endif // GTK 1.x/2.x
1131 void wxTextCtrl::Cut()
1133 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1136 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1140 void wxTextCtrl::Copy()
1142 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1145 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1149 void wxTextCtrl::Paste()
1151 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1154 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1159 void wxTextCtrl::Undo()
1162 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1165 void wxTextCtrl::Redo()
1168 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1171 bool wxTextCtrl::CanUndo() const
1174 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1178 bool wxTextCtrl::CanRedo() const
1181 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1185 // If the return values from and to are the same, there is no
1187 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1189 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1193 bool haveSelection
= FALSE
;
1196 if (m_windowStyle
& wxTE_MULTILINE
)
1198 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer (GTK_TEXT_VIEW (m_text
));
1199 GtkTextIter ifrom
, ito
;
1200 if ( gtk_text_buffer_get_selection_bounds(buffer
, &ifrom
, &ito
) )
1202 haveSelection
= TRUE
;
1203 from
= gtk_text_iter_get_offset(&ifrom
);
1204 to
= gtk_text_iter_get_offset(&ito
);
1207 else // not multi-line
1209 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1212 haveSelection
= TRUE
;
1216 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
1218 haveSelection
= TRUE
;
1219 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
1220 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
1224 if (! haveSelection
)
1225 from
= to
= GetInsertionPoint();
1229 // exchange them to be compatible with wxMSW
1242 bool wxTextCtrl::IsEditable() const
1244 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1247 if (m_windowStyle
& wxTE_MULTILINE
)
1249 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1253 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1256 return GTK_EDITABLE(m_text
)->editable
;
1260 bool wxTextCtrl::IsModified() const
1265 void wxTextCtrl::Clear()
1267 SetValue( wxT("") );
1270 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1272 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1274 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1276 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1277 event
.SetEventObject(this);
1278 event
.SetString(GetValue());
1279 if (GetEventHandler()->ProcessEvent(event
)) return;
1282 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1284 // This will invoke the dialog default action, such
1285 // as the clicking the default button.
1287 wxWindow
*top_frame
= m_parent
;
1288 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1289 top_frame
= top_frame
->GetParent();
1291 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1293 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1295 if (window
->default_widget
)
1297 gtk_widget_activate (window
->default_widget
);
1306 GtkWidget
* wxTextCtrl::GetConnectWidget()
1308 return GTK_WIDGET(m_text
);
1311 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1313 if (m_windowStyle
& wxTE_MULTILINE
)
1316 return window
== gtk_text_view_get_window( GTK_TEXT_VIEW( m_text
), GTK_TEXT_WINDOW_TEXT
); // pure guesswork
1318 return (window
== GTK_TEXT(m_text
)->text_area
);
1323 return (window
== GTK_ENTRY(m_text
)->text_area
);
1327 // the font will change for subsequent text insertiongs
1328 bool wxTextCtrl::SetFont( const wxFont
&font
)
1330 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1332 if ( !wxTextCtrlBase::SetFont(font
) )
1334 // font didn't change, nothing to do
1338 if ( m_windowStyle
& wxTE_MULTILINE
)
1340 m_updateFont
= TRUE
;
1342 m_defaultStyle
.SetFont(font
);
1344 ChangeFontGlobally();
1350 void wxTextCtrl::ChangeFontGlobally()
1352 // this method is very inefficient and hence should be called as rarely as
1354 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1355 _T("shouldn't be called for single line controls") );
1357 wxString value
= GetValue();
1358 if ( !value
.IsEmpty() )
1360 m_updateFont
= FALSE
;
1367 void wxTextCtrl::UpdateFontIfNeeded()
1370 ChangeFontGlobally();
1373 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1375 if ( !wxControl::SetForegroundColour(colour
) )
1378 // update default fg colour too
1379 m_defaultStyle
.SetTextColour(colour
);
1384 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1386 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1388 if ( !wxControl::SetBackgroundColour( colour
) )
1391 if (!m_widget
->window
)
1394 wxColour sysbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1395 if (sysbg
.Red() == colour
.Red() &&
1396 sysbg
.Green() == colour
.Green() &&
1397 sysbg
.Blue() == colour
.Blue())
1399 return FALSE
; // FIXME or TRUE?
1402 if (!m_backgroundColour
.Ok())
1405 if (m_windowStyle
& wxTE_MULTILINE
)
1408 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1411 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1412 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1413 gdk_window_clear( window
);
1417 // change active background color too
1418 m_defaultStyle
.SetBackgroundColour( colour
);
1423 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1425 if ( m_windowStyle
& wxTE_MULTILINE
)
1427 if ( style
.IsDefault() )
1433 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
1434 gint l
= gtk_text_buffer_get_char_count( text_buffer
);
1436 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1437 _T("invalid range in wxTextCtrl::SetStyle") );
1439 GtkTextIter starti
, endi
;
1440 gtk_text_buffer_get_iter_at_offset( text_buffer
, &starti
, start
);
1441 gtk_text_buffer_get_iter_at_offset( text_buffer
, &endi
, end
);
1443 // use the attributes from style which are set in it and fall back
1444 // first to the default style and then to the text control default
1445 // colours for the others
1446 wxTextAttr attr
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
1448 PangoFontDescription
*font_description
= attr
.HasFont()
1449 ? attr
.GetFont().GetNativeFontInfo()->description
1452 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
1455 GdkColor
*colBg
= attr
.HasBackgroundColour()
1456 ? attr
.GetBackgroundColour().GetColor()
1460 tag
= gtk_text_buffer_create_tag( text_buffer
, NULL
, "font-desc", font_description
,
1461 "foreground-gdk", colFg
,
1462 "background-gdk", colBg
, NULL
);
1463 gtk_text_buffer_apply_tag( text_buffer
, tag
, &starti
, &endi
);
1467 // VERY dirty way to do that - removes the required text and re-adds it
1468 // with styling (FIXME)
1470 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1472 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1473 _T("invalid range in wxTextCtrl::SetStyle") );
1475 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1476 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1477 wxString
tmp(text
,*wxConvCurrent
);
1480 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1481 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1484 wxWX2MBbuf buf
= tmp
.mbc_str();
1485 const char *txt
= buf
;
1486 size_t txtlen
= strlen(buf
);
1488 const char *txt
= tmp
;
1489 size_t txtlen
= tmp
.length();
1492 // use the attributes from style which are set in it and fall back
1493 // first to the default style and then to the text control default
1494 // colours for the others
1495 wxGtkTextInsert(m_text
,
1496 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1500 /* does not seem to help under GTK+ 1.2 !!!
1501 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1502 SetInsertionPoint( old_pos
);
1508 // cannot do this for GTK+'s Entry widget
1513 void wxTextCtrl::ApplyWidgetStyle()
1515 if (m_windowStyle
& wxTE_MULTILINE
)
1522 gtk_widget_set_style( m_text
, m_widgetStyle
);
1526 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1531 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1536 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1541 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1546 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1551 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1553 event
.Enable( CanCut() );
1556 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1558 event
.Enable( CanCopy() );
1561 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1563 event
.Enable( CanPaste() );
1566 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1568 event
.Enable( CanUndo() );
1571 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1573 event
.Enable( CanRedo() );
1576 void wxTextCtrl::OnInternalIdle()
1578 wxCursor cursor
= m_cursor
;
1579 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1584 GdkWindow
*window
= (GdkWindow
*) NULL
;
1585 if (HasFlag(wxTE_MULTILINE
))
1586 window
= GTK_TEXT(m_text
)->text_area
;
1588 window
= GTK_ENTRY(m_text
)->text_area
;
1591 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1593 if (!g_globalCursor
.Ok())
1594 cursor
= *wxSTANDARD_CURSOR
;
1596 window
= m_widget
->window
;
1597 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1598 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1602 if (g_delayedFocus
== this)
1604 if (GTK_WIDGET_REALIZED(m_widget
))
1606 gtk_widget_grab_focus( m_widget
);
1607 g_delayedFocus
= NULL
;
1611 if (wxUpdateUIEvent::CanUpdate(this))
1612 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1615 wxSize
wxTextCtrl::DoGetBestSize() const
1617 // FIXME should be different for multi-line controls...
1618 wxSize
ret( wxControl::DoGetBestSize() );
1619 return wxSize(80, ret
.y
);
1622 // ----------------------------------------------------------------------------
1624 // ----------------------------------------------------------------------------
1626 void wxTextCtrl::Freeze()
1629 if ( HasFlag(wxTE_MULTILINE
) )
1631 gtk_text_freeze(GTK_TEXT(m_text
));
1636 void wxTextCtrl::Thaw()
1639 if ( HasFlag(wxTE_MULTILINE
) )
1641 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1643 gtk_text_thaw(GTK_TEXT(m_text
));
1648 // ----------------------------------------------------------------------------
1650 // ----------------------------------------------------------------------------
1652 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1657 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1661 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1664 float value
= adj
->value
+ diff
;
1669 float upper
= adj
->upper
- adj
->page_size
;
1670 if ( value
> upper
)
1673 // did we noticeably change the scroll position?
1674 if ( fabs(adj
->value
- value
) < 0.2 )
1676 // well, this is what Robert does in wxScrollBar, so it must be good...
1682 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1688 bool wxTextCtrl::ScrollLines(int lines
)
1693 GtkAdjustment
*adj
= GetVAdj();
1697 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1698 static const int KEY_SCROLL_PIXELS
= 10;
1700 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1704 bool wxTextCtrl::ScrollPages(int pages
)
1709 GtkAdjustment
*adj
= GetVAdj();
1713 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));