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"
21 #include <sys/types.h>
24 #include <math.h> // for fabs
26 // TODO: reimplement wxTextCtrl using GtkTextView
28 #define GTK_ENABLE_BROKEN // need this to get GtkText at all
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 // ----------------------------------------------------------------------------
53 static void wxGtkTextInsert(GtkWidget
*text
,
54 const wxTextAttr
& attr
,
58 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
61 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
64 GdkColor
*colBg
= attr
.HasBackgroundColour()
65 ? attr
.GetBackgroundColour().GetColor()
68 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
71 // ----------------------------------------------------------------------------
72 // "insert_text" for GtkEntry
73 // ----------------------------------------------------------------------------
76 gtk_insert_text_callback(GtkEditable
*editable
,
77 const gchar
*new_text
,
83 wxapp_install_idle_handler();
85 // we should only be called if we have a max len limit at all
86 GtkEntry
*entry
= GTK_ENTRY (editable
);
88 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
90 // check that we don't overflow the max length limit
92 // FIXME: this doesn't work when we paste a string which is going to be
94 if ( entry
->text_length
== entry
->text_max_length
)
96 // we don't need to run the base class version at all
97 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
99 // remember that the next changed signal is to be ignored to avoid
100 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
101 win
->IgnoreNextTextUpdate();
103 // and generate the correct one ourselves
104 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
105 event
.SetEventObject(win
);
106 event
.SetString(win
->GetValue());
107 win
->GetEventHandler()->ProcessEvent( event
);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
116 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
118 if ( win
->IgnoreTextUpdate() )
121 if (!win
->m_hasVMT
) return;
124 wxapp_install_idle_handler();
127 win
->UpdateFontIfNeeded();
129 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
130 event
.SetEventObject( win
);
131 event
.SetString( win
->GetValue() );
132 win
->GetEventHandler()->ProcessEvent( event
);
135 //-----------------------------------------------------------------------------
136 // "changed" from vertical scrollbar
137 //-----------------------------------------------------------------------------
140 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
142 if (!win
->m_hasVMT
) return;
145 wxapp_install_idle_handler();
147 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;
254 /* a multi-line edit control: create a vertical scrollbar by default and
255 horizontal if requested */
256 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
258 bool bHasHScrollbar
= FALSE
;
261 /* create our control ... */
262 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
264 /* ... and put into the upper left hand corner of the table */
265 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
266 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
267 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
268 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
269 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
272 /* always wrap words */
273 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
276 /* put the horizontal scrollbar in the lower left hand corner */
279 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
280 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
281 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
282 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
285 gtk_widget_show(hscrollbar
);
287 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
288 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
292 /* finally, put the vertical scrollbar in the upper right corner */
293 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
294 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
295 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
297 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
302 /* a single-line text control: no need for scrollbars */
304 m_text
= gtk_entry_new();
307 m_parent
->DoAddChild( this );
309 m_focusWidget
= m_text
;
313 SetFont( parent
->GetFont() );
315 wxSize
size_best( DoGetBestSize() );
316 wxSize
new_size( size
);
317 if (new_size
.x
== -1)
318 new_size
.x
= size_best
.x
;
319 if (new_size
.y
== -1)
320 new_size
.y
= size_best
.y
;
321 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
322 SetSize( new_size
.x
, new_size
.y
);
325 gtk_widget_show(m_text
);
329 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
330 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
333 // only initialize gs_gtk_text_draw once, starting from the next the
334 // klass::draw will already be wxgtk_text_draw
335 if ( !gs_gtk_text_draw
)
338 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
340 gs_gtk_text_draw
= draw
;
342 draw
= wxgtk_text_draw
;
347 if (!value
.IsEmpty())
351 #if !GTK_CHECK_VERSION(1, 2, 0)
352 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
353 // gtk_editable_insert_text()
354 gtk_widget_realize(m_text
);
358 wxWX2MBbuf val
= value
.mbc_str();
359 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
361 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
362 #endif // Unicode/!Unicode
366 /* bring editable's cursor uptodate. bug in GTK. */
367 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
371 if (style
& wxTE_PASSWORD
)
374 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
377 if (style
& wxTE_READONLY
)
380 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
385 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
388 /* we want to be notified about text changes */
389 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
390 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
392 /* we don't set a valid background colour, because the window
393 manager should use a default one */
394 m_backgroundColour
= wxColour();
396 wxColour colFg
= parent
->GetForegroundColour();
397 SetForegroundColour( colFg
);
399 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
401 wxTextAttr
attrDef( colFg
, m_backgroundColour
, parent
->GetFont() );
402 SetDefaultStyle( attrDef
);
409 void wxTextCtrl::CalculateScrollbar()
411 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
413 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
415 if (adj
->upper
- adj
->page_size
< 0.8)
417 if (m_vScrollbarVisible
)
419 gtk_widget_hide( m_vScrollbar
);
420 m_vScrollbarVisible
= FALSE
;
425 if (!m_vScrollbarVisible
)
427 gtk_widget_show( m_vScrollbar
);
428 m_vScrollbarVisible
= TRUE
;
433 wxString
wxTextCtrl::GetValue() const
435 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
438 if (m_windowStyle
& wxTE_MULTILINE
)
440 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
441 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
442 tmp
= wxString(text
,*wxConvCurrent
);
447 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
452 void wxTextCtrl::SetValue( const wxString
&value
)
454 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
456 if (m_windowStyle
& wxTE_MULTILINE
)
458 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
459 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
462 wxWX2MBbuf tmpbuf
= value
.mbc_str();
463 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
465 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
470 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
473 // GRG, Jun/2000: Changed this after a lot of discussion in
474 // the lists. wxWindows 2.2 will have a set of flags to
475 // customize this behaviour.
476 SetInsertionPoint(0);
481 void wxTextCtrl::WriteText( const wxString
&text
)
483 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
489 wxWX2MBbuf buf
= text
.mbc_str();
490 const char *txt
= buf
;
491 size_t txtlen
= strlen(buf
);
493 const char *txt
= text
;
494 size_t txtlen
= text
.length();
497 if ( m_windowStyle
& wxTE_MULTILINE
)
499 // After cursor movements, gtk_text_get_point() is wrong by one.
500 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
502 // always use m_defaultStyle, even if it is empty as otherwise
503 // resetting the style and appending some more text wouldn't work: if
504 // we don't specify the style explicitly, the old style would be used
505 wxGtkTextInsert(m_text
, m_defaultStyle
, txt
, txtlen
);
507 // Bring editable's cursor back uptodate.
508 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
512 // This moves the cursor pos to behind the inserted text.
513 gint len
= GET_EDITABLE_POS(m_text
);
514 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
516 // Bring editable's cursor uptodate.
519 // Bring entry's cursor uptodate.
520 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
526 void wxTextCtrl::AppendText( const wxString
&text
)
528 SetInsertionPointEnd();
532 wxString
wxTextCtrl::GetLineText( long lineNo
) const
534 if (m_windowStyle
& wxTE_MULTILINE
)
536 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
537 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
541 wxString
buf(wxT(""));
544 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
549 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
557 return wxEmptyString
;
562 if (lineNo
== 0) return GetValue();
563 return wxEmptyString
;
567 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
569 /* If you implement this, don't forget to update the documentation!
570 * (file docs/latex/wx/text.tex) */
571 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
574 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
576 if ( m_windowStyle
& wxTE_MULTILINE
)
578 wxString text
= GetValue();
580 // cast to prevent warning. But pos really should've been unsigned.
581 if( (unsigned long)pos
> text
.Len() )
587 const wxChar
* stop
= text
.c_str() + pos
;
588 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
599 else // single line control
601 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
608 // index out of bounds
616 long wxTextCtrl::XYToPosition(long x
, long y
) const
618 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
621 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
627 int wxTextCtrl::GetLineLength(long lineNo
) const
629 wxString str
= GetLineText (lineNo
);
630 return (int) str
.Length();
633 int wxTextCtrl::GetNumberOfLines() const
635 if (m_windowStyle
& wxTE_MULTILINE
)
637 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
638 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
643 for (int i
= 0; i
< len
; i
++ )
650 // currentLine is 0 based, add 1 to get number of lines
651 return currentLine
+ 1;
664 void wxTextCtrl::SetInsertionPoint( long pos
)
666 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
668 if (m_windowStyle
& wxTE_MULTILINE
)
670 /* seems to be broken in GTK 1.0.X:
671 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
673 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
674 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
676 /* we fake a set_point by inserting and deleting. as the user
677 isn't supposed to get to know about thos non-sense, we
678 disconnect so that no events are sent to the user program. */
680 gint tmp
= (gint
)pos
;
681 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
682 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
684 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
685 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
687 /* bring editable's cursor uptodate. another bug in GTK. */
689 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
693 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
695 /* bring editable's cursor uptodate. bug in GTK. */
697 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
701 void wxTextCtrl::SetInsertionPointEnd()
703 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
705 if (m_windowStyle
& wxTE_MULTILINE
)
706 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
708 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
711 void wxTextCtrl::SetEditable( bool editable
)
713 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
715 if (m_windowStyle
& wxTE_MULTILINE
)
716 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
718 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
721 bool wxTextCtrl::Enable( bool enable
)
723 if (!wxWindowBase::Enable(enable
))
729 if (m_windowStyle
& wxTE_MULTILINE
)
731 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
732 OnParentEnable(enable
);
736 gtk_widget_set_sensitive( m_text
, enable
);
742 // wxGTK-specific: called recursively by Enable,
743 // to give widgets an oppprtunity to correct their colours after they
744 // have been changed by Enable
745 void wxTextCtrl::OnParentEnable( bool enable
)
747 // If we have a custom background colour, we use this colour in both
748 // disabled and enabled mode, or we end up with a different colour under the
750 wxColour oldColour
= GetBackgroundColour();
753 // Need to set twice or it'll optimize the useful stuff out
754 if (oldColour
== * wxWHITE
)
755 SetBackgroundColour(*wxBLACK
);
757 SetBackgroundColour(*wxWHITE
);
758 SetBackgroundColour(oldColour
);
762 void wxTextCtrl::DiscardEdits()
767 // ----------------------------------------------------------------------------
768 // max text length support
769 // ----------------------------------------------------------------------------
771 void wxTextCtrl::IgnoreNextTextUpdate()
773 m_ignoreNextUpdate
= TRUE
;
776 bool wxTextCtrl::IgnoreTextUpdate()
778 if ( m_ignoreNextUpdate
)
780 m_ignoreNextUpdate
= FALSE
;
788 void wxTextCtrl::SetMaxLength(unsigned long len
)
790 if ( !HasFlag(wxTE_MULTILINE
) )
792 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
794 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
795 // we had tried to enter more text than allowed by max text length and
796 // the text wasn't really changed
798 // to detect this and generate TEXT_MAXLEN event instead of
799 // TEXT_CHANGED one in this case we also catch "insert_text" signal
801 // when max len is set to 0 we disconnect our handler as it means that
802 // we shouldn't check anything any more
805 gtk_signal_connect( GTK_OBJECT(m_text
),
807 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
812 gtk_signal_disconnect_by_func
815 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
822 void wxTextCtrl::SetSelection( long from
, long to
)
824 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
826 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
827 !GTK_TEXT(m_text
)->line_start_cache
)
829 // tell the programmer that it didn't work
830 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
834 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
837 void wxTextCtrl::ShowPosition( long pos
)
839 if (m_windowStyle
& wxTE_MULTILINE
)
841 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
842 float totalLines
= (float) GetNumberOfLines();
845 PositionToXY(pos
, &posX
, &posY
);
846 float posLine
= (float) posY
;
847 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
848 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
852 long wxTextCtrl::GetInsertionPoint() const
854 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
856 return (long) GET_EDITABLE_POS(m_text
);
859 long wxTextCtrl::GetLastPosition() const
861 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
864 if (m_windowStyle
& wxTE_MULTILINE
)
865 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
867 pos
= GTK_ENTRY(m_text
)->text_length
;
872 void wxTextCtrl::Remove( long from
, long to
)
874 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
876 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
879 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
881 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
883 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
885 if (!value
.IsEmpty())
887 gint pos
= (gint
)from
;
889 wxWX2MBbuf buf
= value
.mbc_str();
890 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
892 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
897 void wxTextCtrl::Cut()
899 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
901 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
904 void wxTextCtrl::Copy()
906 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
908 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
911 void wxTextCtrl::Paste()
913 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
915 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
919 void wxTextCtrl::Undo()
922 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
925 void wxTextCtrl::Redo()
928 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
931 bool wxTextCtrl::CanUndo() const
934 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
938 bool wxTextCtrl::CanRedo() const
941 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
945 // If the return values from and to are the same, there is no
947 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
949 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
953 if ( !gtk_editable_get_selection_bounds(GTK_EDITABLE(m_text
), &from
, &to
) )
955 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
959 to
= GetInsertionPoint();
961 else // got selection
964 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
965 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
970 // exchange them to be compatible with wxMSW
983 bool wxTextCtrl::IsEditable() const
985 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
988 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
990 return GTK_EDITABLE(m_text
)->editable
;
994 bool wxTextCtrl::IsModified() const
999 void wxTextCtrl::Clear()
1001 SetValue( wxT("") );
1004 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1006 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1008 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1010 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1011 event
.SetEventObject(this);
1012 event
.SetString(GetValue());
1013 if (GetEventHandler()->ProcessEvent(event
)) return;
1016 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1018 // This will invoke the dialog default action, such
1019 // as the clicking the default button.
1021 wxWindow
*top_frame
= m_parent
;
1022 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1023 top_frame
= top_frame
->GetParent();
1025 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1027 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1029 if (window
->default_widget
)
1031 gtk_widget_activate (window
->default_widget
);
1040 GtkWidget
* wxTextCtrl::GetConnectWidget()
1042 return GTK_WIDGET(m_text
);
1045 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1047 if (m_windowStyle
& wxTE_MULTILINE
)
1048 return (window
== GTK_TEXT(m_text
)->text_area
);
1050 return (window
== GTK_ENTRY(m_text
)->text_area
);
1053 // the font will change for subsequent text insertiongs
1054 bool wxTextCtrl::SetFont( const wxFont
&font
)
1056 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1058 if ( !wxTextCtrlBase::SetFont(font
) )
1060 // font didn't change, nothing to do
1064 if ( m_windowStyle
& wxTE_MULTILINE
)
1066 m_updateFont
= TRUE
;
1068 m_defaultStyle
.SetFont(font
);
1070 ChangeFontGlobally();
1076 void wxTextCtrl::ChangeFontGlobally()
1078 // this method is very inefficient and hence should be called as rarely as
1080 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1081 _T("shouldn't be called for single line controls") );
1083 wxString value
= GetValue();
1084 if ( !value
.IsEmpty() )
1086 m_updateFont
= FALSE
;
1093 void wxTextCtrl::UpdateFontIfNeeded()
1096 ChangeFontGlobally();
1099 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1101 if ( !wxControl::SetForegroundColour(colour
) )
1104 // update default fg colour too
1105 m_defaultStyle
.SetTextColour(colour
);
1110 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1112 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1114 if ( !wxControl::SetBackgroundColour( colour
) )
1117 if (!m_widget
->window
)
1120 wxColour sysbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1121 if (sysbg
.Red() == colour
.Red() &&
1122 sysbg
.Green() == colour
.Green() &&
1123 sysbg
.Blue() == colour
.Blue())
1125 return FALSE
; // FIXME or TRUE?
1128 if (!m_backgroundColour
.Ok())
1131 if (m_windowStyle
& wxTE_MULTILINE
)
1133 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1136 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1137 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1138 gdk_window_clear( window
);
1141 // change active background color too
1142 m_defaultStyle
.SetBackgroundColour( colour
);
1147 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1149 /* VERY dirty way to do that - removes the required text and re-adds it
1150 with styling (FIXME) */
1151 if ( m_windowStyle
& wxTE_MULTILINE
)
1153 if ( style
.IsDefault() )
1159 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1161 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1162 _T("invalid range in wxTextCtrl::SetStyle") );
1164 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1165 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1166 wxString
tmp(text
,*wxConvCurrent
);
1169 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1170 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1173 wxWX2MBbuf buf
= tmp
.mbc_str();
1174 const char *txt
= buf
;
1175 size_t txtlen
= strlen(buf
);
1177 const char *txt
= tmp
;
1178 size_t txtlen
= tmp
.length();
1181 // use the attributes from style which are set in it and fall back
1182 // first to the default style and then to the text control default
1183 // colours for the others
1184 wxGtkTextInsert(m_text
,
1185 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1189 /* does not seem to help under GTK+ 1.2 !!!
1190 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1191 SetInsertionPoint( old_pos
);
1196 // cannot do this for GTK+'s Entry widget
1201 void wxTextCtrl::ApplyWidgetStyle()
1203 if (m_windowStyle
& wxTE_MULTILINE
)
1210 gtk_widget_set_style( m_text
, m_widgetStyle
);
1214 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1219 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1224 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1229 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1234 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1239 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1241 event
.Enable( CanCut() );
1244 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1246 event
.Enable( CanCopy() );
1249 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1251 event
.Enable( CanPaste() );
1254 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1256 event
.Enable( CanUndo() );
1259 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1261 event
.Enable( CanRedo() );
1264 void wxTextCtrl::OnInternalIdle()
1266 wxCursor cursor
= m_cursor
;
1267 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1271 GdkWindow
*window
= (GdkWindow
*) NULL
;
1272 if (HasFlag(wxTE_MULTILINE
))
1273 window
= GTK_TEXT(m_text
)->text_area
;
1275 window
= GTK_ENTRY(m_text
)->text_area
;
1278 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1280 if (!g_globalCursor
.Ok())
1281 cursor
= *wxSTANDARD_CURSOR
;
1283 window
= m_widget
->window
;
1284 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1285 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1288 if (g_delayedFocus
== this)
1290 if (GTK_WIDGET_REALIZED(m_widget
))
1292 gtk_widget_grab_focus( m_widget
);
1293 g_delayedFocus
= NULL
;
1300 wxSize
wxTextCtrl::DoGetBestSize() const
1302 // FIXME should be different for multi-line controls...
1303 wxSize
ret( wxControl::DoGetBestSize() );
1304 return wxSize(80, ret
.y
);
1307 // ----------------------------------------------------------------------------
1309 // ----------------------------------------------------------------------------
1311 void wxTextCtrl::Freeze()
1313 if ( HasFlag(wxTE_MULTILINE
) )
1315 gtk_text_freeze(GTK_TEXT(m_text
));
1319 void wxTextCtrl::Thaw()
1321 if ( HasFlag(wxTE_MULTILINE
) )
1323 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1325 gtk_text_thaw(GTK_TEXT(m_text
));
1329 // ----------------------------------------------------------------------------
1331 // ----------------------------------------------------------------------------
1333 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1335 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1338 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1340 float value
= adj
->value
+ diff
;
1345 float upper
= adj
->upper
- adj
->page_size
;
1346 if ( value
> upper
)
1349 // did we noticeably change the scroll position?
1350 if ( fabs(adj
->value
- value
) < 0.2 )
1352 // well, this is what Robert does in wxScrollBar, so it must be good...
1358 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1363 bool wxTextCtrl::ScrollLines(int lines
)
1365 GtkAdjustment
*adj
= GetVAdj();
1369 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1370 static const int KEY_SCROLL_PIXELS
= 10;
1372 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1375 bool wxTextCtrl::ScrollPages(int pages
)
1377 GtkAdjustment
*adj
= GetVAdj();
1381 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));