1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/textctrl.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
12 #include "wx/textctrl.h"
20 #include "wx/settings.h"
24 #include "wx/strconv.h"
25 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
26 #include "wx/evtloop.h"
28 #include <sys/types.h>
32 #include "wx/gtk1/private.h"
33 #include <gdk/gdkkeysyms.h>
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern void wxapp_install_idle_handler();
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern wxCursor g_globalCursor
;
47 extern wxWindowGTK
*g_delayedFocus
;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
54 static void wxGtkTextInsert(GtkWidget
*text
,
55 const wxTextAttr
& attr
,
63 tmpFont
= attr
.GetFont();
65 // FIXME: if this crashes because tmpFont goes out of scope and the GdkFont is
66 // deleted, then we need to call gdk_font_ref on font.
67 // This is because attr.GetFont() now returns a temporary font since wxTextAttr
68 // no longer stores a wxFont object, for efficiency.
70 font
= tmpFont
.GetInternalFont();
75 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
78 GdkColor
*colBg
= attr
.HasBackgroundColour()
79 ? attr
.GetBackgroundColour().GetColor()
82 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
86 // ----------------------------------------------------------------------------
87 // "insert_text" for GtkEntry
88 // ----------------------------------------------------------------------------
92 gtk_insert_text_callback(GtkEditable
*editable
,
93 const gchar
*WXUNUSED(new_text
),
94 gint
WXUNUSED(new_text_length
),
95 gint
*WXUNUSED(position
),
99 wxapp_install_idle_handler();
101 // we should only be called if we have a max len limit at all
102 GtkEntry
*entry
= GTK_ENTRY (editable
);
104 wxCHECK_RET( entry
->text_max_length
, wxT("shouldn't be called") );
106 // check that we don't overflow the max length limit
108 // FIXME: this doesn't work when we paste a string which is going to be
110 if ( entry
->text_length
== entry
->text_max_length
)
112 // we don't need to run the base class version at all
113 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
115 // remember that the next changed signal is to be ignored to avoid
116 // generating a dummy wxEVT_TEXT event
117 win
->IgnoreNextTextUpdate();
119 // and generate the correct one ourselves
120 wxCommandEvent
event(wxEVT_TEXT_MAXLEN
, win
->GetId());
121 event
.SetEventObject(win
);
122 event
.SetString(win
->GetValue());
123 win
->HandleWindowEvent( event
);
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
134 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
136 if ( win
->IgnoreTextUpdate() )
139 if (!win
->m_hasVMT
) return;
142 wxapp_install_idle_handler();
145 win
->UpdateFontIfNeeded();
147 wxCommandEvent
event( wxEVT_TEXT
, win
->GetId() );
148 event
.SetEventObject( win
);
149 win
->HandleWindowEvent( event
);
153 //-----------------------------------------------------------------------------
154 // "changed" from vertical scrollbar
155 //-----------------------------------------------------------------------------
159 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
161 if (!win
->m_hasVMT
) return;
164 wxapp_install_idle_handler();
166 win
->CalculateScrollbar();
170 // ----------------------------------------------------------------------------
171 // redraw callback for multiline text
172 // ----------------------------------------------------------------------------
174 // redrawing a GtkText from inside a wxYield() call results in crashes (the
175 // text sample shows it in its "Add lines" command which shows wxProgressDialog
176 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
177 // don't do anything if we're inside wxYield()
180 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
183 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
186 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
188 wxEventLoopBase
* loop
= wxEventLoopBase::GetActive();
189 if ( loop
&& !loop
->IsYielding() )
191 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
192 wxT("infinite recursion in wxgtk_text_draw aborted") );
194 gs_gtk_text_draw(widget
, rect
);
199 //-----------------------------------------------------------------------------
201 //-----------------------------------------------------------------------------
203 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
204 EVT_CHAR(wxTextCtrl::OnChar
)
206 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
207 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
208 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
209 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
210 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
212 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
213 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
214 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
215 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
216 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
219 void wxTextCtrl::Init()
223 SetUpdateFont(false);
228 wxTextCtrl::~wxTextCtrl()
232 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
234 const wxString
&value
,
238 const wxValidator
& validator
,
239 const wxString
&name
)
243 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
246 bool wxTextCtrl::Create( wxWindow
*parent
,
248 const wxString
&value
,
252 const wxValidator
& validator
,
253 const wxString
&name
)
256 m_acceptsFocus
= true;
258 if (!PreCreation( parent
, pos
, size
) ||
259 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
261 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
266 m_vScrollbarVisible
= false;
268 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
272 // create our control ...
273 m_text
= gtk_text_new( NULL
, NULL
);
275 // ... and put into the upper left hand corner of the table
276 bool bHasHScrollbar
= false;
277 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
278 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
279 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
280 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
281 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
285 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
287 // finally, put the vertical scrollbar in the upper right corner
288 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
289 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
290 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
292 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
297 // a single-line text control: no need for scrollbars
299 m_text
= gtk_entry_new();
302 m_parent
->DoAddChild( this );
304 m_focusWidget
= m_text
;
309 gtk_widget_show(m_text
);
313 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
314 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
316 // only initialize gs_gtk_text_draw once, starting from the next the
317 // klass::draw will already be wxgtk_text_draw
318 if ( !gs_gtk_text_draw
)
321 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
323 gs_gtk_text_draw
= draw
;
325 draw
= wxgtk_text_draw
;
331 #if !GTK_CHECK_VERSION(1, 2, 0)
332 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
333 // gtk_editable_insert_text()
334 gtk_widget_realize(m_text
);
339 wxWX2MBbuf val
= value
.mbc_str();
340 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
342 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &tmp
);
347 // Bring editable's cursor uptodate. Bug in GTK.
348 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
352 if (style
& wxTE_PASSWORD
)
355 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
358 if (style
& wxTE_READONLY
)
361 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
366 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
369 // We want to be notified about text changes.
370 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
371 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
373 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
375 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
376 SetDefaultStyle( attrDef
);
382 void wxTextCtrl::CalculateScrollbar()
384 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
386 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
388 if (adj
->upper
- adj
->page_size
< 0.8)
390 if (m_vScrollbarVisible
)
392 gtk_widget_hide( m_vScrollbar
);
393 m_vScrollbarVisible
= false;
398 if (!m_vScrollbarVisible
)
400 gtk_widget_show( m_vScrollbar
);
401 m_vScrollbarVisible
= true;
406 wxString
wxTextCtrl::DoGetValue() const
408 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
411 if (m_windowStyle
& wxTE_MULTILINE
)
413 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
414 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
420 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
426 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
428 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
430 if ( !(flags
& SetValue_SendEvent
) )
432 // do not generate events
433 IgnoreNextTextUpdate();
436 if (m_windowStyle
& wxTE_MULTILINE
)
438 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
439 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
441 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.length(), &len
);
445 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
448 // GRG, Jun/2000: Changed this after a lot of discussion in
449 // the lists. wxWidgets 2.2 will have a set of flags to
450 // customize this behaviour.
451 SetInsertionPoint(0);
456 void wxTextCtrl::WriteText( const wxString
&text
)
458 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
463 // gtk_text_changed_callback() will set m_modified to true but m_modified
464 // shouldn't be changed by the program writing to the text control itself,
465 // so save the old value and restore when we're done
466 bool oldModified
= m_modified
;
468 if ( m_windowStyle
& wxTE_MULTILINE
)
470 // After cursor movements, gtk_text_get_point() is wrong by one.
471 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
473 // always use m_defaultStyle, even if it is empty as otherwise
474 // resetting the style and appending some more text wouldn't work: if
475 // we don't specify the style explicitly, the old style would be used
476 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
477 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.length());
479 // we called wxGtkTextInsert with correct font, no need to do anything
480 // in UpdateFontIfNeeded() any longer
483 SetUpdateFont(false);
486 // Bring editable's cursor back uptodate.
487 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
491 // First remove the selection if there is one
492 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
494 // This moves the cursor pos to behind the inserted text.
495 gint len
= GET_EDITABLE_POS(m_text
);
497 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.length(), &len
);
499 // Bring entry's cursor uptodate.
500 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
503 m_modified
= oldModified
;
506 void wxTextCtrl::AppendText( const wxString
&text
)
508 SetInsertionPointEnd();
512 wxString
wxTextCtrl::GetLineText( long lineNo
) const
514 if (m_windowStyle
& wxTE_MULTILINE
)
516 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
517 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
524 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
529 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
537 return wxEmptyString
;
542 if (lineNo
== 0) return GetValue();
543 return wxEmptyString
;
547 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
549 /* If you implement this, don't forget to update the documentation!
550 * (file docs/latex/wx/text.tex) */
551 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
554 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
556 if ( m_windowStyle
& wxTE_MULTILINE
)
558 wxString text
= GetValue();
560 // cast to prevent warning. But pos really should've been unsigned.
561 if( (unsigned long)pos
> text
.length() )
567 const wxChar
* stop
= text
.c_str() + pos
;
568 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
579 else // single line control
581 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
588 // index out of bounds
596 long wxTextCtrl::XYToPosition(long x
, long y
) const
598 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
601 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
607 int wxTextCtrl::GetLineLength(long lineNo
) const
609 wxString str
= GetLineText (lineNo
);
610 return (int) str
.length();
613 int wxTextCtrl::GetNumberOfLines() const
615 if (m_windowStyle
& wxTE_MULTILINE
)
617 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
618 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
623 for (int i
= 0; i
< len
; i
++ )
630 // currentLine is 0 based, add 1 to get number of lines
631 return currentLine
+ 1;
644 void wxTextCtrl::SetInsertionPoint( long pos
)
646 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
650 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
651 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
653 /* we fake a set_point by inserting and deleting. as the user
654 isn't supposed to get to know about this non-sense, we
655 disconnect so that no events are sent to the user program. */
657 gint tmp
= (gint
)pos
;
658 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
659 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
661 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
662 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
664 // bring editable's cursor uptodate. Bug in GTK.
665 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
669 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
671 // Bring editable's cursor uptodate. Bug in GTK.
672 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
676 void wxTextCtrl::SetInsertionPointEnd()
678 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
680 if (m_windowStyle
& wxTE_MULTILINE
)
682 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
686 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
690 void wxTextCtrl::SetEditable( bool editable
)
692 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
694 if (m_windowStyle
& wxTE_MULTILINE
)
696 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
700 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
704 void wxTextCtrl::DoEnable( bool enable
)
706 if (m_windowStyle
& wxTE_MULTILINE
)
708 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
712 gtk_widget_set_sensitive( m_text
, enable
);
716 void wxTextCtrl::MarkDirty()
721 void wxTextCtrl::DiscardEdits()
726 // ----------------------------------------------------------------------------
727 // max text length support
728 // ----------------------------------------------------------------------------
730 void wxTextCtrl::IgnoreNextTextUpdate()
732 m_ignoreNextUpdate
= true;
735 bool wxTextCtrl::IgnoreTextUpdate()
737 if ( m_ignoreNextUpdate
)
739 m_ignoreNextUpdate
= false;
747 void wxTextCtrl::SetMaxLength(unsigned long len
)
749 if ( !HasFlag(wxTE_MULTILINE
) )
751 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
753 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
754 // we had tried to enter more text than allowed by max text length and
755 // the text wasn't really changed
757 // to detect this and generate TEXT_MAXLEN event instead of
758 // TEXT_CHANGED one in this case we also catch "insert_text" signal
760 // when max len is set to 0 we disconnect our handler as it means that
761 // we shouldn't check anything any more
764 gtk_signal_connect( GTK_OBJECT(m_text
),
766 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
771 gtk_signal_disconnect_by_func
774 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
781 void wxTextCtrl::SetSelection( long from
, long to
)
783 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
785 if (from
== -1 && to
== -1)
788 to
= GetValue().length();
791 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
792 !GTK_TEXT(m_text
)->line_start_cache
)
794 // tell the programmer that it didn't work
795 wxLogDebug(wxT("Can't call SetSelection() before realizing the control"));
799 if (m_windowStyle
& wxTE_MULTILINE
)
801 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
805 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
809 void wxTextCtrl::ShowPosition( long pos
)
811 if (m_windowStyle
& wxTE_MULTILINE
)
813 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
814 float totalLines
= (float) GetNumberOfLines();
817 PositionToXY(pos
, &posX
, &posY
);
818 float posLine
= (float) posY
;
819 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
820 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
824 long wxTextCtrl::GetInsertionPoint() const
826 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
827 return (long) GET_EDITABLE_POS(m_text
);
830 wxTextPos
wxTextCtrl::GetLastPosition() const
832 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
836 if (m_windowStyle
& wxTE_MULTILINE
)
838 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
842 pos
= GTK_ENTRY(m_text
)->text_length
;
848 void wxTextCtrl::Remove( long from
, long to
)
850 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
851 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
854 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
856 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
862 gint pos
= (gint
)from
;
864 wxWX2MBbuf buf
= value
.mbc_str();
865 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
867 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &pos
);
868 #endif // wxUSE_UNICODE
872 void wxTextCtrl::Cut()
874 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
875 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
878 void wxTextCtrl::Copy()
880 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
881 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
884 void wxTextCtrl::Paste()
886 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
887 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
891 void wxTextCtrl::Undo()
894 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
897 void wxTextCtrl::Redo()
900 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
903 bool wxTextCtrl::CanUndo() const
906 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
910 bool wxTextCtrl::CanRedo() const
913 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
917 // If the return values from and to are the same, there is no
919 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
921 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
925 bool haveSelection
= false;
927 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
929 haveSelection
= true;
930 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
931 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
934 if (! haveSelection
)
935 from
= to
= GetInsertionPoint();
939 // exchange them to be compatible with wxMSW
952 bool wxTextCtrl::IsEditable() const
954 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
955 return GTK_EDITABLE(m_text
)->editable
;
958 bool wxTextCtrl::IsModified() const
963 void wxTextCtrl::Clear()
965 SetValue( wxEmptyString
);
968 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
970 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
972 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
974 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
975 event
.SetEventObject(this);
976 event
.SetString(GetValue());
977 if (HandleWindowEvent(event
)) return;
980 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
982 // This will invoke the dialog default action, such
983 // as the clicking the default button.
985 wxWindow
*top_frame
= m_parent
;
986 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
987 top_frame
= top_frame
->GetParent();
989 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
991 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
993 if (window
->default_widget
)
995 gtk_widget_activate (window
->default_widget
);
1004 GtkWidget
* wxTextCtrl::GetConnectWidget()
1006 return GTK_WIDGET(m_text
);
1009 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1011 if (m_windowStyle
& wxTE_MULTILINE
)
1013 return (window
== GTK_TEXT(m_text
)->text_area
);
1017 return (window
== GTK_ENTRY(m_text
)->text_area
);
1021 // the font will change for subsequent text insertiongs
1022 bool wxTextCtrl::SetFont( const wxFont
&font
)
1024 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1026 if ( !wxTextCtrlBase::SetFont(font
) )
1028 // font didn't change, nothing to do
1032 if ( m_windowStyle
& wxTE_MULTILINE
)
1034 SetUpdateFont(true);
1036 m_defaultStyle
.SetFont(font
);
1038 ChangeFontGlobally();
1044 void wxTextCtrl::ChangeFontGlobally()
1046 // this method is very inefficient and hence should be called as rarely as
1048 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1050 wxT("shouldn't be called for single line controls") );
1052 wxString value
= GetValue();
1053 if ( !value
.empty() )
1055 SetUpdateFont(false);
1062 void wxTextCtrl::UpdateFontIfNeeded()
1065 ChangeFontGlobally();
1068 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1070 if ( !wxControl::SetForegroundColour(colour
) )
1073 // update default fg colour too
1074 m_defaultStyle
.SetTextColour(colour
);
1079 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1081 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1083 if ( !wxControl::SetBackgroundColour( colour
) )
1086 if (!m_widget
->window
)
1089 if (!m_backgroundColour
.IsOk())
1092 if (m_windowStyle
& wxTE_MULTILINE
)
1094 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1097 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1098 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1099 gdk_window_clear( window
);
1102 // change active background color too
1103 m_defaultStyle
.SetBackgroundColour( colour
);
1108 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1110 if ( m_windowStyle
& wxTE_MULTILINE
)
1112 if ( style
.IsDefault() )
1118 // VERY dirty way to do that - removes the required text and re-adds it
1119 // with styling (FIXME)
1121 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1123 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1124 wxT("invalid range in wxTextCtrl::SetStyle") );
1126 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1127 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1128 wxString
tmp(text
,*wxConvCurrent
);
1131 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1132 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1135 wxWX2MBbuf buf
= tmp
.mbc_str();
1136 const char *txt
= buf
;
1137 size_t txtlen
= strlen(buf
);
1139 const char *txt
= tmp
;
1140 size_t txtlen
= tmp
.length();
1143 // use the attributes from style which are set in it and fall back
1144 // first to the default style and then to the text control default
1145 // colours for the others
1146 wxGtkTextInsert(m_text
,
1147 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1151 /* does not seem to help under GTK+ 1.2 !!!
1152 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1153 SetInsertionPoint( old_pos
);
1159 // cannot do this for GTK+'s Entry widget
1163 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1165 gtk_widget_modify_style(m_text
, style
);
1168 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1173 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1178 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1183 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1188 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1193 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1195 event
.Enable( CanCut() );
1198 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1200 event
.Enable( CanCopy() );
1203 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1205 event
.Enable( CanPaste() );
1208 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1210 event
.Enable( CanUndo() );
1213 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1215 event
.Enable( CanRedo() );
1218 void wxTextCtrl::OnInternalIdle()
1220 wxCursor cursor
= m_cursor
;
1221 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
1225 GdkWindow
*window
= NULL
;
1226 if (HasFlag(wxTE_MULTILINE
))
1227 window
= GTK_TEXT(m_text
)->text_area
;
1229 window
= GTK_ENTRY(m_text
)->text_area
;
1232 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1234 if (!g_globalCursor
.IsOk())
1235 cursor
= *wxSTANDARD_CURSOR
;
1237 window
= m_widget
->window
;
1238 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1239 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1242 if (g_delayedFocus
== this)
1244 if (GTK_WIDGET_REALIZED(m_widget
))
1246 gtk_widget_grab_focus( m_widget
);
1247 g_delayedFocus
= NULL
;
1251 if (wxUpdateUIEvent::CanUpdate(this))
1252 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1255 wxSize
wxTextCtrl::DoGetBestSize() const
1257 // FIXME should be different for multi-line controls...
1258 wxSize
ret( wxControl::DoGetBestSize() );
1259 wxSize
best(80, ret
.y
);
1260 CacheBestSize(best
);
1264 // ----------------------------------------------------------------------------
1266 // ----------------------------------------------------------------------------
1268 void wxTextCtrl::DoFreeze()
1270 if ( HasFlag(wxTE_MULTILINE
) )
1272 gtk_text_freeze(GTK_TEXT(m_text
));
1276 void wxTextCtrl::DoThaw()
1278 if ( HasFlag(wxTE_MULTILINE
) )
1280 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1282 gtk_text_thaw(GTK_TEXT(m_text
));
1286 // ----------------------------------------------------------------------------
1288 // ----------------------------------------------------------------------------
1290 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1292 if ( !IsMultiLine() )
1295 return GTK_TEXT(m_text
)->vadj
;
1298 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1300 float value
= adj
->value
+ diff
;
1305 float upper
= adj
->upper
- adj
->page_size
;
1306 if ( value
> upper
)
1309 // did we noticeably change the scroll position?
1310 if ( fabs(adj
->value
- value
) < 0.2 )
1312 // well, this is what Robert does in wxScrollBar, so it must be good...
1317 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1322 bool wxTextCtrl::ScrollLines(int lines
)
1324 GtkAdjustment
*adj
= GetVAdj();
1328 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1329 int diff
= 10*lines
;
1331 return DoScroll(adj
, diff
);
1334 bool wxTextCtrl::ScrollPages(int pages
)
1336 GtkAdjustment
*adj
= GetVAdj();
1340 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
1346 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1348 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);