1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "textctrl.h"
14 #include "wx/textctrl.h"
18 #include "wx/settings.h"
20 #include "wx/strconv.h"
22 #include <sys/types.h>
25 #include <math.h> // for fabs
27 #include "wx/gtk/private.h"
28 #include <gdk/gdkkeysyms.h>
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern bool g_blockEventsOnDrag
;
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 // ----------------------------------------------------------------------------
74 gtk_insert_text_callback(GtkEditable
*editable
,
75 const gchar
*new_text
,
81 wxapp_install_idle_handler();
83 // we should only be called if we have a max len limit at all
84 GtkEntry
*entry
= GTK_ENTRY (editable
);
86 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
88 // check that we don't overflow the max length limit
90 // FIXME: this doesn't work when we paste a string which is going to be
92 if ( entry
->text_length
== entry
->text_max_length
)
94 // we don't need to run the base class version at all
95 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
97 // remember that the next changed signal is to be ignored to avoid
98 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
99 win
->IgnoreNextTextUpdate();
101 // and generate the correct one ourselves
102 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
103 event
.SetEventObject(win
);
104 event
.SetString(win
->GetValue());
105 win
->GetEventHandler()->ProcessEvent( event
);
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
114 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
116 if ( win
->IgnoreTextUpdate() )
119 if (!win
->m_hasVMT
) return;
122 wxapp_install_idle_handler();
125 win
->UpdateFontIfNeeded();
127 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
128 event
.SetEventObject( win
);
129 event
.SetString( win
->GetValue() );
130 win
->GetEventHandler()->ProcessEvent( event
);
133 //-----------------------------------------------------------------------------
134 // "changed" from vertical scrollbar
135 //-----------------------------------------------------------------------------
139 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
141 if (!win
->m_hasVMT
) return;
144 wxapp_install_idle_handler();
146 win
->CalculateScrollbar();
150 // ----------------------------------------------------------------------------
151 // redraw callback for multiline text
152 // ----------------------------------------------------------------------------
156 // redrawing a GtkText from inside a wxYield() call results in crashes (the
157 // text sample shows it in its "Add lines" command which shows wxProgressDialog
158 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
159 // don't do anything if we're inside wxYield()
161 extern bool wxIsInsideYield
;
164 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
167 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
170 void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
172 if ( !wxIsInsideYield
)
174 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
175 _T("infinite recursion in wxgtk_text_draw aborted") );
177 gs_gtk_text_draw(widget
, rect
);
181 #endif // __WXGTK20__
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 m_updateFont
= FALSE
;
211 m_vScrollbar
= (GtkWidget
*)NULL
;
214 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
216 const wxString
&value
,
220 const wxValidator
& validator
,
221 const wxString
&name
)
225 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
228 bool wxTextCtrl::Create( wxWindow
*parent
,
230 const wxString
&value
,
234 const wxValidator
& validator
,
235 const wxString
&name
)
238 m_acceptsFocus
= TRUE
;
240 if (!PreCreation( parent
, pos
, size
) ||
241 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
243 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
248 m_vScrollbarVisible
= FALSE
;
250 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
253 GtkTextBuffer
*buffer
= NULL
;
260 m_text
= gtk_text_view_new();
262 buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
264 // create scrolled window
265 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
266 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
267 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
269 // Insert view into scrolled window
270 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
272 // Global settings which can be overridden by tags, I guess.
273 if (HasFlag( wxHSCROLL
))
274 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), GTK_WRAP_NONE
);
276 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), GTK_WRAP_WORD
);
278 if (!HasFlag(wxNO_BORDER
))
279 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget
), GTK_SHADOW_IN
);
281 // create our control ...
282 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
284 // ... and put into the upper left hand corner of the table
285 bool bHasHScrollbar
= FALSE
;
286 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
287 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
288 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
289 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
290 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
294 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
296 // finally, put the vertical scrollbar in the upper right corner
297 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
298 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
299 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
301 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
307 // a single-line text control: no need for scrollbars
309 m_text
= gtk_entry_new();
312 m_parent
->DoAddChild( this );
314 m_focusWidget
= m_text
;
318 SetFont( parent
->GetFont() );
320 wxSize
size_best( DoGetBestSize() );
321 wxSize
new_size( size
);
322 if (new_size
.x
== -1)
323 new_size
.x
= size_best
.x
;
324 if (new_size
.y
== -1)
325 new_size
.y
= size_best
.y
;
326 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
327 SetSize( new_size
.x
, new_size
.y
);
330 gtk_widget_show(m_text
);
335 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
336 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
338 // only initialize gs_gtk_text_draw once, starting from the next the
339 // klass::draw will already be wxgtk_text_draw
340 if ( !gs_gtk_text_draw
)
343 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
345 gs_gtk_text_draw
= draw
;
347 draw
= wxgtk_text_draw
;
352 if (!value
.IsEmpty())
358 #if !GTK_CHECK_VERSION(1, 2, 0)
359 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
360 // gtk_editable_insert_text()
361 gtk_widget_realize(m_text
);
366 wxWX2MBbuf val
= value
.mbc_str();
367 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
369 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
374 // Bring editable's cursor uptodate. Bug in GTK.
375 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
381 if (style
& wxTE_PASSWORD
)
384 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
387 if (style
& wxTE_READONLY
)
390 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
393 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text
), FALSE
);
400 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
404 // We want to be notified about text changes.
408 g_signal_connect( G_OBJECT(buffer
), "changed",
409 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
414 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
415 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
418 // we don't set a valid background colour, because the window
419 // manager should use a default one
420 m_backgroundColour
= wxColour();
422 wxColour colFg
= parent
->GetForegroundColour();
423 SetForegroundColour( colFg
);
425 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
428 wxTextAttr
attrDef( colFg
, m_backgroundColour
, parent
->GetFont() );
429 SetDefaultStyle( attrDef
);
437 void wxTextCtrl::CalculateScrollbar()
440 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
442 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
444 if (adj
->upper
- adj
->page_size
< 0.8)
446 if (m_vScrollbarVisible
)
448 gtk_widget_hide( m_vScrollbar
);
449 m_vScrollbarVisible
= FALSE
;
454 if (!m_vScrollbarVisible
)
456 gtk_widget_show( m_vScrollbar
);
457 m_vScrollbarVisible
= TRUE
;
463 wxString
wxTextCtrl::GetValue() const
465 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
468 if (m_windowStyle
& wxTE_MULTILINE
)
471 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
474 gtk_text_buffer_get_start_iter( text_buffer
, &start
);
476 gtk_text_buffer_get_end_iter( text_buffer
, &end
);
477 gchar
*text
= gtk_text_buffer_get_text( text_buffer
, &start
, &end
, TRUE
);
480 wxWCharBuffer
buffer( wxConvUTF8
.cMB2WX( text
) );
482 wxCharBuffer
buffer( wxConvLocal
.cWC2WX( wxConvUTF8
.cMB2WC( text
) ) );
488 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
489 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
496 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
502 void wxTextCtrl::SetValue( const wxString
&value
)
504 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
506 if (m_windowStyle
& wxTE_MULTILINE
)
511 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( value
) );
513 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( value
) ) );
515 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
516 gtk_text_buffer_set_text( text_buffer
, buffer
, strlen(buffer
) );
519 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
520 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
522 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
527 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
530 // GRG, Jun/2000: Changed this after a lot of discussion in
531 // the lists. wxWindows 2.2 will have a set of flags to
532 // customize this behaviour.
533 SetInsertionPoint(0);
538 void wxTextCtrl::WriteText( const wxString
&text
)
540 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
545 if ( m_windowStyle
& wxTE_MULTILINE
)
550 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( text
) );
552 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) ) );
554 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
555 // TODO: call wahtever is needed to delete the selection
556 gtk_text_buffer_insert_at_cursor( text_buffer
, buffer
, strlen(buffer
) );
559 // After cursor movements, gtk_text_get_point() is wrong by one.
560 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
562 // always use m_defaultStyle, even if it is empty as otherwise
563 // resetting the style and appending some more text wouldn't work: if
564 // we don't specify the style explicitly, the old style would be used
565 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
566 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.Len());
568 // Bring editable's cursor back uptodate.
569 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
570 #endif // GTK 1.x/2.0
574 // First remove the selection if there is one
575 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
577 // This moves the cursor pos to behind the inserted text.
578 gint len
= GET_EDITABLE_POS(m_text
);
583 wxCharBuffer
buffer( wxConvUTF8
.cWX2MB( text
) );
585 wxCharBuffer
buffer( wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) ) );
587 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
590 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.Len(), &len
);
593 // Bring entry's cursor uptodate.
594 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
600 void wxTextCtrl::AppendText( const wxString
&text
)
602 SetInsertionPointEnd();
606 wxString
wxTextCtrl::GetLineText( long lineNo
) const
608 if (m_windowStyle
& wxTE_MULTILINE
)
611 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
612 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
616 wxString
buf(wxT(""));
619 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
624 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
633 return wxEmptyString
;
638 if (lineNo
== 0) return GetValue();
639 return wxEmptyString
;
643 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
645 /* If you implement this, don't forget to update the documentation!
646 * (file docs/latex/wx/text.tex) */
647 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
650 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
652 if ( m_windowStyle
& wxTE_MULTILINE
)
654 wxString text
= GetValue();
656 // cast to prevent warning. But pos really should've been unsigned.
657 if( (unsigned long)pos
> text
.Len() )
663 const wxChar
* stop
= text
.c_str() + pos
;
664 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
675 else // single line control
677 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
684 // index out of bounds
692 long wxTextCtrl::XYToPosition(long x
, long y
) const
694 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
697 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
703 int wxTextCtrl::GetLineLength(long lineNo
) const
705 wxString str
= GetLineText (lineNo
);
706 return (int) str
.Length();
709 int wxTextCtrl::GetNumberOfLines() const
711 if (m_windowStyle
& wxTE_MULTILINE
)
714 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
716 return gtk_text_buffer_get_line_count( buffer
);
718 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
719 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
724 for (int i
= 0; i
< len
; i
++ )
731 // currentLine is 0 based, add 1 to get number of lines
732 return currentLine
+ 1;
746 void wxTextCtrl::SetInsertionPoint( long pos
)
748 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
750 if (m_windowStyle
& wxTE_MULTILINE
)
753 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
755 gtk_text_buffer_get_iter_at_offset( text_buffer
, &iter
, pos
);
756 gtk_text_buffer_place_cursor( text_buffer
, &iter
);
758 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
759 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
761 /* we fake a set_point by inserting and deleting. as the user
762 isn't supposed to get to know about this non-sense, we
763 disconnect so that no events are sent to the user program. */
765 gint tmp
= (gint
)pos
;
766 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
767 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
769 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
770 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
772 // bring editable's cursor uptodate. Bug in GTK.
773 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
778 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
780 // Bring editable's cursor uptodate. Bug in GTK.
781 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
785 void wxTextCtrl::SetInsertionPointEnd()
787 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
789 if (m_windowStyle
& wxTE_MULTILINE
)
792 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
794 gtk_text_buffer_get_end_iter( text_buffer
, &end
);
795 gtk_text_buffer_place_cursor( text_buffer
, &end
);
797 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
802 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
806 void wxTextCtrl::SetEditable( bool editable
)
808 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
810 if (m_windowStyle
& wxTE_MULTILINE
)
813 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
815 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
820 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
824 bool wxTextCtrl::Enable( bool enable
)
826 if (!wxWindowBase::Enable(enable
))
832 if (m_windowStyle
& wxTE_MULTILINE
)
835 SetEditable( enable
);
837 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
838 OnParentEnable(enable
);
843 gtk_widget_set_sensitive( m_text
, enable
);
849 // wxGTK-specific: called recursively by Enable,
850 // to give widgets an oppprtunity to correct their colours after they
851 // have been changed by Enable
852 void wxTextCtrl::OnParentEnable( bool enable
)
854 // If we have a custom background colour, we use this colour in both
855 // disabled and enabled mode, or we end up with a different colour under the
857 wxColour oldColour
= GetBackgroundColour();
860 // Need to set twice or it'll optimize the useful stuff out
861 if (oldColour
== * wxWHITE
)
862 SetBackgroundColour(*wxBLACK
);
864 SetBackgroundColour(*wxWHITE
);
865 SetBackgroundColour(oldColour
);
869 void wxTextCtrl::DiscardEdits()
874 // ----------------------------------------------------------------------------
875 // max text length support
876 // ----------------------------------------------------------------------------
878 void wxTextCtrl::IgnoreNextTextUpdate()
880 m_ignoreNextUpdate
= TRUE
;
883 bool wxTextCtrl::IgnoreTextUpdate()
885 if ( m_ignoreNextUpdate
)
887 m_ignoreNextUpdate
= FALSE
;
895 void wxTextCtrl::SetMaxLength(unsigned long len
)
897 if ( !HasFlag(wxTE_MULTILINE
) )
899 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
901 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
902 // we had tried to enter more text than allowed by max text length and
903 // the text wasn't really changed
905 // to detect this and generate TEXT_MAXLEN event instead of
906 // TEXT_CHANGED one in this case we also catch "insert_text" signal
908 // when max len is set to 0 we disconnect our handler as it means that
909 // we shouldn't check anything any more
912 gtk_signal_connect( GTK_OBJECT(m_text
),
914 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
919 gtk_signal_disconnect_by_func
922 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
929 void wxTextCtrl::SetSelection( long from
, long to
)
931 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
933 if (from
== -1 && to
== -1)
936 to
= GetValue().Length();
940 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
941 !GTK_TEXT(m_text
)->line_start_cache
)
943 // tell the programmer that it didn't work
944 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
949 if (m_windowStyle
& wxTE_MULTILINE
)
954 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
959 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
963 void wxTextCtrl::ShowPosition( long pos
)
966 if (m_windowStyle
& wxTE_MULTILINE
)
968 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
969 float totalLines
= (float) GetNumberOfLines();
972 PositionToXY(pos
, &posX
, &posY
);
973 float posLine
= (float) posY
;
974 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
975 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
980 long wxTextCtrl::GetInsertionPoint() const
982 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
985 if (m_windowStyle
& wxTE_MULTILINE
)
987 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
989 // There is no direct accessor for the cursor, but
990 // internally, the cursor is the "mark" called
991 // "insert" in the text view's btree structure.
993 GtkTextMark
*mark
= gtk_text_buffer_get_insert( text_buffer
);
995 gtk_text_buffer_get_iter_at_mark( text_buffer
, &cursor
, mark
);
997 return gtk_text_iter_get_offset( &cursor
);
1002 return (long) GET_EDITABLE_POS(m_text
);
1006 long wxTextCtrl::GetLastPosition() const
1008 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1012 if (m_windowStyle
& wxTE_MULTILINE
)
1015 GtkTextBuffer
*text_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
1017 gtk_text_buffer_get_end_iter( text_buffer
, &end
);
1019 pos
= gtk_text_iter_get_offset( &end
);
1021 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
1026 pos
= GTK_ENTRY(m_text
)->text_length
;
1032 void wxTextCtrl::Remove( long from
, long to
)
1034 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1037 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1041 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1043 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1046 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1048 if (!value
.IsEmpty())
1050 gint pos
= (gint
)from
;
1052 wxWX2MBbuf buf
= value
.mbc_str();
1053 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
1055 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
1061 void wxTextCtrl::Cut()
1063 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1066 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1070 void wxTextCtrl::Copy()
1072 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1075 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1079 void wxTextCtrl::Paste()
1081 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1084 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
1089 void wxTextCtrl::Undo()
1092 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1095 void wxTextCtrl::Redo()
1098 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1101 bool wxTextCtrl::CanUndo() const
1104 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1108 bool wxTextCtrl::CanRedo() const
1111 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1115 // If the return values from and to are the same, there is no
1117 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1119 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1123 if ( !gtk_editable_get_selection_bounds(GTK_EDITABLE(m_text
), &from
, &to
) )
1125 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
1129 to
= GetInsertionPoint();
1131 else // got selection
1134 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
1135 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
1140 // exchange them to be compatible with wxMSW
1153 bool wxTextCtrl::IsEditable() const
1155 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1158 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1160 return GTK_EDITABLE(m_text
)->editable
;
1164 bool wxTextCtrl::IsModified() const
1169 void wxTextCtrl::Clear()
1171 SetValue( wxT("") );
1174 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1176 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1178 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1180 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1181 event
.SetEventObject(this);
1182 event
.SetString(GetValue());
1183 if (GetEventHandler()->ProcessEvent(event
)) return;
1186 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1188 // This will invoke the dialog default action, such
1189 // as the clicking the default button.
1191 wxWindow
*top_frame
= m_parent
;
1192 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1193 top_frame
= top_frame
->GetParent();
1195 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1197 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1199 if (window
->default_widget
)
1201 gtk_widget_activate (window
->default_widget
);
1210 GtkWidget
* wxTextCtrl::GetConnectWidget()
1212 return GTK_WIDGET(m_text
);
1215 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1217 if (m_windowStyle
& wxTE_MULTILINE
)
1220 return window
== gtk_text_view_get_window( GTK_TEXT_VIEW( m_text
), GTK_TEXT_WINDOW_TEXT
); // pure guesswork
1222 return (window
== GTK_TEXT(m_text
)->text_area
);
1227 return (window
== GTK_ENTRY(m_text
)->text_area
);
1231 // the font will change for subsequent text insertiongs
1232 bool wxTextCtrl::SetFont( const wxFont
&font
)
1234 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1236 if ( !wxTextCtrlBase::SetFont(font
) )
1238 // font didn't change, nothing to do
1242 if ( m_windowStyle
& wxTE_MULTILINE
)
1244 m_updateFont
= TRUE
;
1246 m_defaultStyle
.SetFont(font
);
1248 ChangeFontGlobally();
1254 void wxTextCtrl::ChangeFontGlobally()
1256 // this method is very inefficient and hence should be called as rarely as
1258 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1259 _T("shouldn't be called for single line controls") );
1261 wxString value
= GetValue();
1262 if ( !value
.IsEmpty() )
1264 m_updateFont
= FALSE
;
1271 void wxTextCtrl::UpdateFontIfNeeded()
1274 ChangeFontGlobally();
1277 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1279 if ( !wxControl::SetForegroundColour(colour
) )
1282 // update default fg colour too
1283 m_defaultStyle
.SetTextColour(colour
);
1288 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1290 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1292 if ( !wxControl::SetBackgroundColour( colour
) )
1295 if (!m_widget
->window
)
1298 wxColour sysbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1299 if (sysbg
.Red() == colour
.Red() &&
1300 sysbg
.Green() == colour
.Green() &&
1301 sysbg
.Blue() == colour
.Blue())
1303 return FALSE
; // FIXME or TRUE?
1306 if (!m_backgroundColour
.Ok())
1309 if (m_windowStyle
& wxTE_MULTILINE
)
1312 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1315 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1316 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1317 gdk_window_clear( window
);
1321 // change active background color too
1322 m_defaultStyle
.SetBackgroundColour( colour
);
1327 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1329 /* VERY dirty way to do that - removes the required text and re-adds it
1330 with styling (FIXME) */
1331 if ( m_windowStyle
& wxTE_MULTILINE
)
1334 if ( style
.IsDefault() )
1340 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1342 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1343 _T("invalid range in wxTextCtrl::SetStyle") );
1345 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1346 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1347 wxString
tmp(text
,*wxConvCurrent
);
1350 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1351 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1354 wxWX2MBbuf buf
= tmp
.mbc_str();
1355 const char *txt
= buf
;
1356 size_t txtlen
= strlen(buf
);
1358 const char *txt
= tmp
;
1359 size_t txtlen
= tmp
.length();
1362 // use the attributes from style which are set in it and fall back
1363 // first to the default style and then to the text control default
1364 // colours for the others
1365 wxGtkTextInsert(m_text
,
1366 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1370 /* does not seem to help under GTK+ 1.2 !!!
1371 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1372 SetInsertionPoint( old_pos
);
1378 // cannot do this for GTK+'s Entry widget
1383 void wxTextCtrl::ApplyWidgetStyle()
1385 if (m_windowStyle
& wxTE_MULTILINE
)
1392 gtk_widget_set_style( m_text
, m_widgetStyle
);
1396 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1401 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1406 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1411 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1416 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1421 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1423 event
.Enable( CanCut() );
1426 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1428 event
.Enable( CanCopy() );
1431 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1433 event
.Enable( CanPaste() );
1436 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1438 event
.Enable( CanUndo() );
1441 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1443 event
.Enable( CanRedo() );
1446 void wxTextCtrl::OnInternalIdle()
1448 wxCursor cursor
= m_cursor
;
1449 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1454 GdkWindow
*window
= (GdkWindow
*) NULL
;
1455 if (HasFlag(wxTE_MULTILINE
))
1456 window
= GTK_TEXT(m_text
)->text_area
;
1458 window
= GTK_ENTRY(m_text
)->text_area
;
1461 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1463 if (!g_globalCursor
.Ok())
1464 cursor
= *wxSTANDARD_CURSOR
;
1466 window
= m_widget
->window
;
1467 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1468 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1472 if (g_delayedFocus
== this)
1474 if (GTK_WIDGET_REALIZED(m_widget
))
1476 gtk_widget_grab_focus( m_widget
);
1477 g_delayedFocus
= NULL
;
1484 wxSize
wxTextCtrl::DoGetBestSize() const
1486 // FIXME should be different for multi-line controls...
1487 wxSize
ret( wxControl::DoGetBestSize() );
1488 return wxSize(80, ret
.y
);
1491 // ----------------------------------------------------------------------------
1493 // ----------------------------------------------------------------------------
1495 void wxTextCtrl::Freeze()
1498 if ( HasFlag(wxTE_MULTILINE
) )
1500 gtk_text_freeze(GTK_TEXT(m_text
));
1505 void wxTextCtrl::Thaw()
1508 if ( HasFlag(wxTE_MULTILINE
) )
1510 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1512 gtk_text_thaw(GTK_TEXT(m_text
));
1517 // ----------------------------------------------------------------------------
1519 // ----------------------------------------------------------------------------
1521 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1526 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1530 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1533 float value
= adj
->value
+ diff
;
1538 float upper
= adj
->upper
- adj
->page_size
;
1539 if ( value
> upper
)
1542 // did we noticeably change the scroll position?
1543 if ( fabs(adj
->value
- value
) < 0.2 )
1545 // well, this is what Robert does in wxScrollBar, so it must be good...
1551 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1557 bool wxTextCtrl::ScrollLines(int lines
)
1562 GtkAdjustment
*adj
= GetVAdj();
1566 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1567 static const int KEY_SCROLL_PIXELS
= 10;
1569 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1573 bool wxTextCtrl::ScrollPages(int pages
)
1578 GtkAdjustment
*adj
= GetVAdj();
1582 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));