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>
27 #include "gdk/gdkkeysyms.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
41 extern wxCursor g_globalCursor
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
48 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
50 if (!win
->m_hasVMT
) return;
53 wxapp_install_idle_handler();
56 win
->UpdateFontIfNeeded();
58 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
59 event
.SetString( win
->GetValue() );
60 event
.SetEventObject( win
);
61 win
->GetEventHandler()->ProcessEvent( event
);
64 //-----------------------------------------------------------------------------
65 // "changed" from vertical scrollbar
66 //-----------------------------------------------------------------------------
69 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
71 if (!win
->m_hasVMT
) return;
74 wxapp_install_idle_handler();
76 win
->CalculateScrollbar();
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 extern wxWindow
*g_focusWindow
;
84 extern bool g_blockEventsOnDrag
;
85 // extern bool g_isIdle;
87 static gint
gtk_text_focus_in_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
92 wxapp_install_idle_handler();
94 if (!win
->m_hasVMT
) return FALSE
;
95 if (g_blockEventsOnDrag
) return FALSE
;
99 wxPanel
*panel
= wxDynamicCast(win
->GetParent(), wxPanel
);
102 panel
->SetLastFocus(win
);
107 gdk_im_begin(win
->m_ic
, win
->m_wxwindow
->window
);
112 // caret needs to be informed about focus change
113 wxCaret
*caret
= win
->GetCaret();
118 #endif // wxUSE_CARET
121 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
122 event
.SetEventObject( win
);
124 if (win
->GetEventHandler()->ProcessEvent( event
))
126 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus_in_event" );
133 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
137 static gint
gtk_text_focus_out_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
141 wxapp_install_idle_handler();
144 if (!win
->m_hasVMT
) return FALSE
;
145 if (g_blockEventsOnDrag
) return FALSE
;
148 // if the focus goes out of our app alltogether, OnIdle() will send
149 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
150 // g_sendActivateEvent to -1
151 g_sendActivateEvent
= 0;
154 wxWindow
*winFocus
= wxFindFocusedChild(win
);
158 g_focusWindow
= (wxWindow
*)NULL
;
167 // caret needs to be informed about focus change
168 wxCaret
*caret
= win
->GetCaret();
171 caret
->OnKillFocus();
173 #endif // wxUSE_CARET
176 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
177 event
.SetEventObject( win
);
179 if (win
->GetEventHandler()->ProcessEvent( event
))
181 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus_out_event" );
188 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
192 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
194 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
195 EVT_CHAR(wxTextCtrl::OnChar
)
197 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
198 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
199 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
200 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
201 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
203 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
204 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
205 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
206 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
207 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
210 void wxTextCtrl::Init()
213 m_updateFont
= FALSE
;
215 m_vScrollbar
= (GtkWidget
*)NULL
;
218 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
220 const wxString
&value
,
224 const wxValidator
& validator
,
225 const wxString
&name
)
229 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
232 bool wxTextCtrl::Create( wxWindow
*parent
,
234 const wxString
&value
,
238 const wxValidator
& validator
,
239 const wxString
&name
)
242 m_acceptsFocus
= TRUE
;
244 if (!PreCreation( parent
, pos
, size
) ||
245 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
247 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
252 m_vScrollbarVisible
= FALSE
;
254 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
257 #if (GTK_MINOR_VERSION > 2)
258 /* a multi-line edit control: create a vertical scrollbar by default and
259 horizontal if requested */
260 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
262 bool bHasHScrollbar
= FALSE
;
265 /* create our control ... */
266 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
268 /* ... and put into the upper left hand corner of the table */
269 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
270 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
271 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
272 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
273 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
276 /* always wrap words */
277 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
279 #if (GTK_MINOR_VERSION > 2)
280 /* put the horizontal scrollbar in the lower left hand corner */
283 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
284 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
285 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
286 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
289 gtk_widget_show(hscrollbar
);
291 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
292 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
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
),
306 /* a single-line text control: no need for scrollbars */
308 m_text
= gtk_entry_new();
311 m_parent
->DoAddChild( this );
315 SetFont( parent
->GetFont() );
317 wxSize
size_best( DoGetBestSize() );
318 wxSize
new_size( size
);
319 if (new_size
.x
== -1)
320 new_size
.x
= size_best
.x
;
321 if (new_size
.y
== -1)
322 new_size
.y
= size_best
.y
;
323 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
324 SetSize( new_size
.x
, new_size
.y
);
327 gtk_widget_show(m_text
);
331 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
332 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
334 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_in_event",
335 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
337 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_out_event",
338 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
342 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_in_event",
343 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
345 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_out_event",
346 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
349 if (!value
.IsEmpty())
353 #if GTK_MINOR_VERSION == 0
354 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
355 // gtk_editable_insert_text()
356 gtk_widget_realize(m_text
);
360 wxWX2MBbuf val
= value
.mbc_str();
361 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
363 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
364 #endif // Unicode/!Unicode
368 /* bring editable's cursor uptodate. bug in GTK. */
370 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
374 if (style
& wxTE_PASSWORD
)
377 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
380 if (style
& wxTE_READONLY
)
383 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
388 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
391 /* we want to be notified about text changes */
392 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
393 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
395 /* we don't set a valid background colour, because the window
396 manager should use a default one */
397 m_backgroundColour
= wxColour();
399 wxColour colFg
= parent
->GetForegroundColour();
400 SetForegroundColour( colFg
);
402 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
404 // FIXME: is the bg colour correct here?
405 wxTextAttr
attrDef( colFg
,
406 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
),
408 SetDefaultStyle( attrDef
);
415 void wxTextCtrl::CalculateScrollbar()
417 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
419 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
421 if (adj
->upper
- adj
->page_size
< 0.8)
423 if (m_vScrollbarVisible
)
425 gtk_widget_hide( m_vScrollbar
);
426 m_vScrollbarVisible
= FALSE
;
431 if (!m_vScrollbarVisible
)
433 gtk_widget_show( m_vScrollbar
);
434 m_vScrollbarVisible
= TRUE
;
439 wxString
wxTextCtrl::GetValue() const
441 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
444 if (m_windowStyle
& wxTE_MULTILINE
)
446 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
447 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
448 tmp
= wxString(text
,*wxConvCurrent
);
453 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
458 void wxTextCtrl::SetValue( const wxString
&value
)
460 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
462 if (m_windowStyle
& wxTE_MULTILINE
)
464 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
465 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
468 wxWX2MBbuf tmpbuf
= value
.mbc_str();
469 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
471 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
476 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
479 // GRG, Jun/2000: Changed this after a lot of discussion in
480 // the lists. wxWindows 2.2 will have a set of flags to
481 // customize this behaviour.
482 SetInsertionPoint(0);
487 void wxTextCtrl::WriteText( const wxString
&text
)
489 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
495 wxWX2MBbuf buf
= text
.mbc_str();
496 const char *txt
= buf
;
497 size_t txtlen
= strlen(buf
);
499 const char *txt
= text
;
500 size_t txtlen
= text
.length();
503 if ( m_windowStyle
& wxTE_MULTILINE
)
505 /* this moves the cursor pos to behind the inserted text */
506 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
508 // if we have any special style, use it
509 if ( !m_defaultStyle
.IsDefault() )
511 GdkFont
*font
= m_defaultStyle
.HasFont()
512 ? m_defaultStyle
.GetFont().GetInternalFont()
515 GdkColor
*colFg
= m_defaultStyle
.HasTextColour()
516 ? m_defaultStyle
.GetTextColour().GetColor()
519 GdkColor
*colBg
= m_defaultStyle
.HasBackgroundColour()
520 ? m_defaultStyle
.GetBackgroundColour().GetColor()
523 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
527 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
530 /* bring editable's cursor uptodate. bug in GTK. */
531 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
535 /* this moves the cursor pos to behind the inserted text */
536 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
537 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
539 /* bring editable's cursor uptodate. bug in GTK. */
540 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
542 /* bring entry's cursor uptodate. bug in GTK. */
543 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
547 void wxTextCtrl::AppendText( const wxString
&text
)
549 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
555 wxWX2MBbuf buf
= text
.mbc_str();
556 const char *txt
= buf
;
557 size_t txtlen
= strlen(buf
);
559 const char *txt
= text
;
560 size_t txtlen
= text
.length();
563 if (m_windowStyle
& wxTE_MULTILINE
)
565 if ( !m_defaultStyle
.IsDefault() )
567 wxFont font
= m_defaultStyle
.HasFont() ? m_defaultStyle
.GetFont()
569 GdkFont
*fnt
= font
.Ok() ? font
.GetInternalFont() : NULL
;
571 wxColour col
= m_defaultStyle
.HasTextColour()
572 ? m_defaultStyle
.GetTextColour()
573 : m_foregroundColour
;
574 GdkColor
*colFg
= col
.Ok() ? col
.GetColor() : NULL
;
576 col
= m_defaultStyle
.HasBackgroundColour()
577 ? m_defaultStyle
.GetBackgroundColour()
578 : m_backgroundColour
;
579 GdkColor
*colBg
= col
.Ok() ? col
.GetColor() : NULL
;
581 gtk_text_insert( GTK_TEXT(m_text
), fnt
, colFg
, colBg
, txt
, txtlen
);
585 /* we'll insert at the last position */
586 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
587 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
590 /* bring editable's cursor uptodate. bug in GTK. */
591 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
595 gtk_entry_append_text( GTK_ENTRY(m_text
), txt
);
599 wxString
wxTextCtrl::GetLineText( long lineNo
) const
601 if (m_windowStyle
& wxTE_MULTILINE
)
603 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
604 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
608 wxString
buf(wxT(""));
611 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
616 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
623 return wxEmptyString
;
627 if (lineNo
== 0) return GetValue();
628 return wxEmptyString
;
632 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
634 /* If you implement this, don't forget to update the documentation!
635 * (file docs/latex/wx/text.tex) */
636 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
639 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
641 if ( m_windowStyle
& wxTE_MULTILINE
)
643 wxString text
= GetValue();
645 // cast to prevent warning. But pos really should've been unsigned.
646 if( (unsigned long)pos
> text
.Len() )
652 const wxChar
* stop
= text
.c_str() + pos
;
653 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
664 else // single line control
666 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
673 // index out of bounds
681 long wxTextCtrl::XYToPosition(long x
, long y
) const
683 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
686 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
692 int wxTextCtrl::GetLineLength(long lineNo
) const
694 wxString str
= GetLineText (lineNo
);
695 return (int) str
.Length();
698 int wxTextCtrl::GetNumberOfLines() const
700 if (m_windowStyle
& wxTE_MULTILINE
)
702 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
703 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
708 for (int i
= 0; i
< len
; i
++ )
715 // currentLine is 0 based, add 1 to get number of lines
716 return currentLine
+ 1;
729 void wxTextCtrl::SetInsertionPoint( long pos
)
731 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
733 if (m_windowStyle
& wxTE_MULTILINE
)
735 /* seems to be broken in GTK 1.0.X:
736 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
738 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
739 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
741 /* we fake a set_point by inserting and deleting. as the user
742 isn't supposed to get to know about thos non-sense, we
743 disconnect so that no events are sent to the user program. */
745 gint tmp
= (gint
)pos
;
746 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
747 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
749 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
750 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
752 /* bring editable's cursor uptodate. another bug in GTK. */
754 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
758 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
760 /* bring editable's cursor uptodate. bug in GTK. */
762 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
766 void wxTextCtrl::SetInsertionPointEnd()
768 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
770 if (m_windowStyle
& wxTE_MULTILINE
)
771 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
773 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
776 void wxTextCtrl::SetEditable( bool editable
)
778 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
780 if (m_windowStyle
& wxTE_MULTILINE
)
781 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
783 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
786 bool wxTextCtrl::Enable( bool enable
)
788 if (!wxWindowBase::Enable(enable
))
794 if (m_windowStyle
& wxTE_MULTILINE
)
796 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
797 OnParentEnable(enable
);
801 gtk_widget_set_sensitive( m_text
, enable
);
807 // wxGTK-specific: called recursively by Enable,
808 // to give widgets an oppprtunity to correct their colours after they
809 // have been changed by Enable
810 void wxTextCtrl::OnParentEnable( bool enable
)
812 // If we have a custom background colour, we use this colour in both
813 // disabled and enabled mode, or we end up with a different colour under the
815 wxColour oldColour
= GetBackgroundColour();
818 // Need to set twice or it'll optimize the useful stuff out
819 if (oldColour
== * wxWHITE
)
820 SetBackgroundColour(*wxBLACK
);
822 SetBackgroundColour(*wxWHITE
);
823 SetBackgroundColour(oldColour
);
827 void wxTextCtrl::DiscardEdits()
832 void wxTextCtrl::SetSelection( long from
, long to
)
834 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
836 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
837 !GTK_TEXT(m_text
)->line_start_cache
)
839 // tell the programmer that it didn't work
840 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
844 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
847 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
849 // SetInsertionPoint( pos );
852 long wxTextCtrl::GetInsertionPoint() const
854 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
856 return (long) GTK_EDITABLE(m_text
)->current_pos
;
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 #if (GTK_MINOR_VERSION > 0)
902 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
904 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
908 void wxTextCtrl::Copy()
910 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
912 #if (GTK_MINOR_VERSION > 0)
913 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
915 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
919 void wxTextCtrl::Paste()
921 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
923 #if (GTK_MINOR_VERSION > 0)
924 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
926 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
931 void wxTextCtrl::Undo()
934 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
937 void wxTextCtrl::Redo()
940 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
943 bool wxTextCtrl::CanUndo() const
946 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
950 bool wxTextCtrl::CanRedo() const
953 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
957 // If the return values from and to are the same, there is no
959 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
961 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
964 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
967 to
= GetInsertionPoint();
969 else // got selection
971 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
972 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
976 // exchange them to be compatible with wxMSW
989 bool wxTextCtrl::IsEditable() const
991 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
993 return GTK_EDITABLE(m_text
)->editable
;
996 bool wxTextCtrl::IsModified() const
1001 void wxTextCtrl::Clear()
1003 SetValue( wxT("") );
1006 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1008 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1010 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1012 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1013 event
.SetEventObject(this);
1014 event
.SetString(GetValue());
1015 if (GetEventHandler()->ProcessEvent(event
)) return;
1018 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1020 wxWindow
*top_frame
= m_parent
;
1021 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1022 top_frame
= top_frame
->GetParent();
1023 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1025 if (window
->default_widget
)
1027 gtk_widget_activate (window
->default_widget
);
1035 GtkWidget
* wxTextCtrl::GetConnectWidget()
1037 return GTK_WIDGET(m_text
);
1040 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1042 if (m_windowStyle
& wxTE_MULTILINE
)
1043 return (window
== GTK_TEXT(m_text
)->text_area
);
1045 return (window
== GTK_ENTRY(m_text
)->text_area
);
1048 // the font will change for subsequent text insertiongs
1049 bool wxTextCtrl::SetFont( const wxFont
&font
)
1051 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1053 if ( !wxWindowBase::SetFont(font
) )
1055 // font didn't change, nothing to do
1059 if ( m_windowStyle
& wxTE_MULTILINE
)
1061 m_updateFont
= TRUE
;
1063 m_defaultStyle
.SetFont(font
);
1065 ChangeFontGlobally();
1071 void wxTextCtrl::ChangeFontGlobally()
1073 // this method is very inefficient and hence should be called as rarely as
1075 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1076 _T("shouldn't be called for single line controls") );
1078 wxString value
= GetValue();
1079 if ( !value
.IsEmpty() )
1084 m_updateFont
= FALSE
;
1088 void wxTextCtrl::UpdateFontIfNeeded()
1091 ChangeFontGlobally();
1094 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1096 if ( !wxControl::SetForegroundColour(colour
) )
1099 // update default fg colour too
1100 m_defaultStyle
.SetTextColour(colour
);
1105 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1107 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1109 if ( !wxControl::SetBackgroundColour( colour
) )
1112 if (!m_widget
->window
)
1115 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1116 if (sysbg
.Red() == colour
.Red() &&
1117 sysbg
.Green() == colour
.Green() &&
1118 sysbg
.Blue() == colour
.Blue())
1120 return FALSE
; // FIXME or TRUE?
1123 if (!m_backgroundColour
.Ok())
1126 if (m_windowStyle
& wxTE_MULTILINE
)
1128 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1131 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1132 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1133 gdk_window_clear( window
);
1136 // change active background color too
1137 m_defaultStyle
.SetBackgroundColour( colour
);
1142 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
&style
)
1144 /* VERY dirty way to do that - removes the required text and re-adds it
1145 with styling (FIXME) */
1146 if ( m_windowStyle
& wxTE_MULTILINE
)
1148 if ( style
.IsDefault() )
1154 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1156 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1157 _T("invalid range in wxTextCtrl::SetStyle") );
1159 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1160 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1161 wxString
tmp(text
,*wxConvCurrent
);
1164 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1165 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1168 wxWX2MBbuf buf
= tmp
.mbc_str();
1169 const char *txt
= buf
;
1170 size_t txtlen
= strlen(buf
);
1172 const char *txt
= tmp
;
1173 size_t txtlen
= tmp
.length();
1176 GdkFont
*font
= style
.HasFont()
1177 ? style
.GetFont().GetInternalFont()
1180 GdkColor
*colFg
= style
.HasTextColour()
1181 ? style
.GetTextColour().GetColor()
1184 GdkColor
*colBg
= style
.HasBackgroundColour()
1185 ? style
.GetBackgroundColour().GetColor()
1188 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
1190 /* does not seem to help under GTK+ 1.2 !!!
1191 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1192 SetInsertionPoint( old_pos
);
1197 // cannot do this for GTK+'s Entry widget
1202 void wxTextCtrl::ApplyWidgetStyle()
1204 if (m_windowStyle
& wxTE_MULTILINE
)
1211 gtk_widget_set_style( m_text
, m_widgetStyle
);
1215 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1220 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1225 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1230 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1235 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1240 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1242 event
.Enable( CanCut() );
1245 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1247 event
.Enable( CanCopy() );
1250 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1252 event
.Enable( CanPaste() );
1255 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1257 event
.Enable( CanUndo() );
1260 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1262 event
.Enable( CanRedo() );
1265 void wxTextCtrl::OnInternalIdle()
1267 wxCursor cursor
= m_cursor
;
1268 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1272 GdkWindow
*window
= (GdkWindow
*) NULL
;
1273 if (HasFlag(wxTE_MULTILINE
))
1274 window
= GTK_TEXT(m_text
)->text_area
;
1276 window
= GTK_ENTRY(m_text
)->text_area
;
1279 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1281 if (!g_globalCursor
.Ok())
1282 cursor
= *wxSTANDARD_CURSOR
;
1284 window
= m_widget
->window
;
1285 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1286 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1292 wxSize
wxTextCtrl::DoGetBestSize() const
1294 // FIXME should be different for multi-line controls...
1295 wxSize
ret( wxControl::DoGetBestSize() );
1296 return wxSize(80, ret
.y
);
1299 // ----------------------------------------------------------------------------
1301 // ----------------------------------------------------------------------------
1303 void wxTextCtrl::Freeze()
1305 if ( HasFlag(wxTE_MULTILINE
) )
1307 gtk_text_freeze(GTK_TEXT(m_text
));
1311 void wxTextCtrl::Thaw()
1313 if ( HasFlag(wxTE_MULTILINE
) )
1315 gtk_text_thaw(GTK_TEXT(m_text
));
1319 // ----------------------------------------------------------------------------
1321 // ----------------------------------------------------------------------------
1323 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1325 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1328 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1330 float value
= adj
->value
+ diff
;
1335 float upper
= adj
->upper
- adj
->page_size
;
1336 if ( value
> upper
)
1339 // did we noticeably change the scroll position?
1340 if ( fabs(adj
->value
- value
) < 0.2 )
1342 // well, this is what Robert does in wxScrollBar, so it must be good...
1348 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1353 bool wxTextCtrl::ScrollLines(int lines
)
1355 GtkAdjustment
*adj
= GetVAdj();
1359 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1360 static const int KEY_SCROLL_PIXELS
= 10;
1362 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1365 bool wxTextCtrl::ScrollPages(int pages
)
1367 GtkAdjustment
*adj
= GetVAdj();
1371 return DoScroll(adj
, pages
*adj
->page_increment
);