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
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
;
44 // ----------------------------------------------------------------------------
45 // "insert_text" for GtkEntry
46 // ----------------------------------------------------------------------------
49 gtk_insert_text_callback(GtkEditable
*editable
,
50 const gchar
*new_text
,
55 // we should only be called if we have a max len limit at all
56 GtkEntry
*entry
= GTK_ENTRY (editable
);
58 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
60 // check that we don't overflow the max length limit
62 // FIXME: this doesn't work when we paste a string which is going to be
64 if ( entry
->text_length
== entry
->text_max_length
)
66 // we don't need to run the base class version at all
67 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
69 // remember that the next changed signal is to be ignored to avoid
70 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
71 win
->IgnoreNextTextUpdate();
73 // and generate the correct one ourselves
74 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
75 event
.SetEventObject(win
);
76 event
.SetString(win
->GetValue());
77 win
->GetEventHandler()->ProcessEvent( event
);
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
86 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
88 if ( win
->IgnoreTextUpdate() )
91 if (!win
->m_hasVMT
) return;
94 wxapp_install_idle_handler();
97 win
->UpdateFontIfNeeded();
99 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
100 event
.SetEventObject( win
);
101 event
.SetString( win
->GetValue() );
102 win
->GetEventHandler()->ProcessEvent( event
);
105 //-----------------------------------------------------------------------------
106 // "changed" from vertical scrollbar
107 //-----------------------------------------------------------------------------
110 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
112 if (!win
->m_hasVMT
) return;
115 wxapp_install_idle_handler();
117 win
->CalculateScrollbar();
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
124 extern wxWindow
*g_focusWindow
;
125 extern bool g_blockEventsOnDrag
;
126 // extern bool g_isIdle;
128 static gint
gtk_text_focus_in_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
133 wxapp_install_idle_handler();
135 if (!win
->m_hasVMT
) return FALSE
;
136 if (g_blockEventsOnDrag
) return FALSE
;
140 // notify the parent that we got the focus
141 wxChildFocusEvent
eventFocus(win
);
142 (void)win
->GetEventHandler()->ProcessEvent(eventFocus
);
146 gdk_im_begin(win
->m_ic
, win
->m_wxwindow
->window
);
151 // caret needs to be informed about focus change
152 wxCaret
*caret
= win
->GetCaret();
157 #endif // wxUSE_CARET
160 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
161 event
.SetEventObject( win
);
163 if (win
->GetEventHandler()->ProcessEvent( event
))
171 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
175 static gint
gtk_text_focus_out_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
179 wxapp_install_idle_handler();
182 if (!win
->m_hasVMT
) return FALSE
;
183 if (g_blockEventsOnDrag
) return FALSE
;
186 // if the focus goes out of our app alltogether, OnIdle() will send
187 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
188 // g_sendActivateEvent to -1
189 g_sendActivateEvent
= 0;
192 wxWindow
*winFocus
= wxFindFocusedChild(win
);
196 g_focusWindow
= (wxWindow
*)NULL
;
205 // caret needs to be informed about focus change
206 wxCaret
*caret
= win
->GetCaret();
209 caret
->OnKillFocus();
211 #endif // wxUSE_CARET
214 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
215 event
.SetEventObject( win
);
217 if (win
->GetEventHandler()->ProcessEvent( event
))
225 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
229 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
231 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
232 EVT_CHAR(wxTextCtrl::OnChar
)
234 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
235 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
236 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
237 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
238 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
240 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
241 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
242 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
243 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
244 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
247 void wxTextCtrl::Init()
251 m_updateFont
= FALSE
;
253 m_vScrollbar
= (GtkWidget
*)NULL
;
256 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
258 const wxString
&value
,
262 const wxValidator
& validator
,
263 const wxString
&name
)
267 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
270 bool wxTextCtrl::Create( wxWindow
*parent
,
272 const wxString
&value
,
276 const wxValidator
& validator
,
277 const wxString
&name
)
280 m_acceptsFocus
= TRUE
;
282 if (!PreCreation( parent
, pos
, size
) ||
283 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
285 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
290 m_vScrollbarVisible
= FALSE
;
292 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
295 #if (GTK_MINOR_VERSION > 2)
296 /* a multi-line edit control: create a vertical scrollbar by default and
297 horizontal if requested */
298 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
300 bool bHasHScrollbar
= FALSE
;
303 /* create our control ... */
304 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
306 /* ... and put into the upper left hand corner of the table */
307 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
308 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
309 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
310 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
311 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
314 /* always wrap words */
315 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
317 #if (GTK_MINOR_VERSION > 2)
318 /* put the horizontal scrollbar in the lower left hand corner */
321 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
322 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
323 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
324 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
327 gtk_widget_show(hscrollbar
);
329 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
330 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
334 /* finally, put the vertical scrollbar in the upper right corner */
335 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
336 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
337 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
339 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
344 /* a single-line text control: no need for scrollbars */
346 m_text
= gtk_entry_new();
349 m_parent
->DoAddChild( this );
353 SetFont( parent
->GetFont() );
355 wxSize
size_best( DoGetBestSize() );
356 wxSize
new_size( size
);
357 if (new_size
.x
== -1)
358 new_size
.x
= size_best
.x
;
359 if (new_size
.y
== -1)
360 new_size
.y
= size_best
.y
;
361 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
362 SetSize( new_size
.x
, new_size
.y
);
365 gtk_widget_show(m_text
);
369 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
370 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
372 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_in_event",
373 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
375 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_out_event",
376 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
380 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_in_event",
381 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
383 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_out_event",
384 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
387 if (!value
.IsEmpty())
391 #if GTK_MINOR_VERSION == 0
392 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
393 // gtk_editable_insert_text()
394 gtk_widget_realize(m_text
);
398 wxWX2MBbuf val
= value
.mbc_str();
399 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
401 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
402 #endif // Unicode/!Unicode
406 /* bring editable's cursor uptodate. bug in GTK. */
408 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
412 if (style
& wxTE_PASSWORD
)
415 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
418 if (style
& wxTE_READONLY
)
421 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
426 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
429 /* we want to be notified about text changes */
430 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
431 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
433 /* we don't set a valid background colour, because the window
434 manager should use a default one */
435 m_backgroundColour
= wxColour();
437 wxColour colFg
= parent
->GetForegroundColour();
438 SetForegroundColour( colFg
);
440 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
442 // FIXME: is the bg colour correct here?
443 wxTextAttr
attrDef( colFg
,
444 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
),
446 SetDefaultStyle( attrDef
);
453 void wxTextCtrl::CalculateScrollbar()
455 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
457 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
459 if (adj
->upper
- adj
->page_size
< 0.8)
461 if (m_vScrollbarVisible
)
463 gtk_widget_hide( m_vScrollbar
);
464 m_vScrollbarVisible
= FALSE
;
469 if (!m_vScrollbarVisible
)
471 gtk_widget_show( m_vScrollbar
);
472 m_vScrollbarVisible
= TRUE
;
477 wxString
wxTextCtrl::GetValue() const
479 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
482 if (m_windowStyle
& wxTE_MULTILINE
)
484 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
485 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
486 tmp
= wxString(text
,*wxConvCurrent
);
491 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
496 void wxTextCtrl::SetValue( const wxString
&value
)
498 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
500 if (m_windowStyle
& wxTE_MULTILINE
)
502 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
503 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
506 wxWX2MBbuf tmpbuf
= value
.mbc_str();
507 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
509 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
514 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
517 // GRG, Jun/2000: Changed this after a lot of discussion in
518 // the lists. wxWindows 2.2 will have a set of flags to
519 // customize this behaviour.
520 SetInsertionPoint(0);
525 void wxTextCtrl::WriteText( const wxString
&text
)
527 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
533 wxWX2MBbuf buf
= text
.mbc_str();
534 const char *txt
= buf
;
535 size_t txtlen
= strlen(buf
);
537 const char *txt
= text
;
538 size_t txtlen
= text
.length();
541 if ( m_windowStyle
& wxTE_MULTILINE
)
543 /* this moves the cursor pos to behind the inserted text */
544 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
546 // if we have any special style, use it
547 if ( !m_defaultStyle
.IsDefault() )
549 GdkFont
*font
= m_defaultStyle
.HasFont()
550 ? m_defaultStyle
.GetFont().GetInternalFont()
553 GdkColor
*colFg
= m_defaultStyle
.HasTextColour()
554 ? m_defaultStyle
.GetTextColour().GetColor()
557 GdkColor
*colBg
= m_defaultStyle
.HasBackgroundColour()
558 ? m_defaultStyle
.GetBackgroundColour().GetColor()
561 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
565 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
568 /* bring editable's cursor uptodate. bug in GTK. */
569 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
573 /* this moves the cursor pos to behind the inserted text */
574 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
575 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
577 /* bring editable's cursor uptodate. bug in GTK. */
578 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
580 /* bring entry's cursor uptodate. bug in GTK. */
581 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
585 void wxTextCtrl::AppendText( const wxString
&text
)
587 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
593 wxWX2MBbuf buf
= text
.mbc_str();
594 const char *txt
= buf
;
595 size_t txtlen
= strlen(buf
);
597 const char *txt
= text
;
598 size_t txtlen
= text
.length();
601 if (m_windowStyle
& wxTE_MULTILINE
)
603 if ( !m_defaultStyle
.IsDefault() )
605 wxFont font
= m_defaultStyle
.HasFont() ? m_defaultStyle
.GetFont()
607 GdkFont
*fnt
= font
.Ok() ? font
.GetInternalFont() : NULL
;
609 wxColour col
= m_defaultStyle
.HasTextColour()
610 ? m_defaultStyle
.GetTextColour()
611 : m_foregroundColour
;
612 GdkColor
*colFg
= col
.Ok() ? col
.GetColor() : NULL
;
614 col
= m_defaultStyle
.HasBackgroundColour()
615 ? m_defaultStyle
.GetBackgroundColour()
616 : m_backgroundColour
;
617 GdkColor
*colBg
= col
.Ok() ? col
.GetColor() : NULL
;
619 gtk_text_insert( GTK_TEXT(m_text
), fnt
, colFg
, colBg
, txt
, txtlen
);
623 /* we'll insert at the last position */
624 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
625 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
628 /* bring editable's cursor uptodate. bug in GTK. */
629 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
633 gtk_entry_append_text( GTK_ENTRY(m_text
), txt
);
637 wxString
wxTextCtrl::GetLineText( long lineNo
) const
639 if (m_windowStyle
& wxTE_MULTILINE
)
641 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
642 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
646 wxString
buf(wxT(""));
649 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
654 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
661 return wxEmptyString
;
665 if (lineNo
== 0) return GetValue();
666 return wxEmptyString
;
670 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
672 /* If you implement this, don't forget to update the documentation!
673 * (file docs/latex/wx/text.tex) */
674 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
677 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
679 if ( m_windowStyle
& wxTE_MULTILINE
)
681 wxString text
= GetValue();
683 // cast to prevent warning. But pos really should've been unsigned.
684 if( (unsigned long)pos
> text
.Len() )
690 const wxChar
* stop
= text
.c_str() + pos
;
691 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
702 else // single line control
704 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
711 // index out of bounds
719 long wxTextCtrl::XYToPosition(long x
, long y
) const
721 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
724 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
730 int wxTextCtrl::GetLineLength(long lineNo
) const
732 wxString str
= GetLineText (lineNo
);
733 return (int) str
.Length();
736 int wxTextCtrl::GetNumberOfLines() const
738 if (m_windowStyle
& wxTE_MULTILINE
)
740 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
741 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
746 for (int i
= 0; i
< len
; i
++ )
753 // currentLine is 0 based, add 1 to get number of lines
754 return currentLine
+ 1;
767 void wxTextCtrl::SetInsertionPoint( long pos
)
769 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
771 if (m_windowStyle
& wxTE_MULTILINE
)
773 /* seems to be broken in GTK 1.0.X:
774 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
776 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
777 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
779 /* we fake a set_point by inserting and deleting. as the user
780 isn't supposed to get to know about thos non-sense, we
781 disconnect so that no events are sent to the user program. */
783 gint tmp
= (gint
)pos
;
784 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
785 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
787 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
788 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
790 /* bring editable's cursor uptodate. another bug in GTK. */
792 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
796 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
798 /* bring editable's cursor uptodate. bug in GTK. */
800 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
804 void wxTextCtrl::SetInsertionPointEnd()
806 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
808 if (m_windowStyle
& wxTE_MULTILINE
)
809 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
811 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
814 void wxTextCtrl::SetEditable( bool editable
)
816 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
818 if (m_windowStyle
& wxTE_MULTILINE
)
819 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
821 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
)
834 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
835 OnParentEnable(enable
);
839 gtk_widget_set_sensitive( m_text
, enable
);
845 // wxGTK-specific: called recursively by Enable,
846 // to give widgets an oppprtunity to correct their colours after they
847 // have been changed by Enable
848 void wxTextCtrl::OnParentEnable( bool enable
)
850 // If we have a custom background colour, we use this colour in both
851 // disabled and enabled mode, or we end up with a different colour under the
853 wxColour oldColour
= GetBackgroundColour();
856 // Need to set twice or it'll optimize the useful stuff out
857 if (oldColour
== * wxWHITE
)
858 SetBackgroundColour(*wxBLACK
);
860 SetBackgroundColour(*wxWHITE
);
861 SetBackgroundColour(oldColour
);
865 void wxTextCtrl::DiscardEdits()
870 // ----------------------------------------------------------------------------
871 // max text length support
872 // ----------------------------------------------------------------------------
874 void wxTextCtrl::IgnoreNextTextUpdate()
876 m_ignoreNextUpdate
= TRUE
;
879 bool wxTextCtrl::IgnoreTextUpdate()
881 if ( m_ignoreNextUpdate
)
883 m_ignoreNextUpdate
= FALSE
;
891 void wxTextCtrl::SetMaxLength(unsigned long len
)
893 if ( !HasFlag(wxTE_MULTILINE
) )
895 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
897 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
898 // we had tried to enter more text than allowed by max text length and
899 // the text wasn't really changed
901 // to detect this and generate TEXT_MAXLEN event instead of
902 // TEXT_CHANGED one in this case we also catch "insert_text" signal
904 // when max len is set to 0 we disconnect our handler as it means that
905 // we shouldn't check anything any more
908 gtk_signal_connect( GTK_OBJECT(m_text
),
910 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
915 gtk_signal_disconnect_by_func
918 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
925 void wxTextCtrl::SetSelection( long from
, long to
)
927 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
929 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
930 !GTK_TEXT(m_text
)->line_start_cache
)
932 // tell the programmer that it didn't work
933 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
937 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
940 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
942 // SetInsertionPoint( pos );
945 long wxTextCtrl::GetInsertionPoint() const
947 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
949 return (long) GTK_EDITABLE(m_text
)->current_pos
;
952 long wxTextCtrl::GetLastPosition() const
954 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
957 if (m_windowStyle
& wxTE_MULTILINE
)
958 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
960 pos
= GTK_ENTRY(m_text
)->text_length
;
965 void wxTextCtrl::Remove( long from
, long to
)
967 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
969 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
972 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
974 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
976 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
978 if (!value
.IsEmpty())
980 gint pos
= (gint
)from
;
982 wxWX2MBbuf buf
= value
.mbc_str();
983 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
985 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
990 void wxTextCtrl::Cut()
992 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
994 #if (GTK_MINOR_VERSION > 0)
995 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
997 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
1001 void wxTextCtrl::Copy()
1003 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1005 #if (GTK_MINOR_VERSION > 0)
1006 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
1008 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
1012 void wxTextCtrl::Paste()
1014 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1016 #if (GTK_MINOR_VERSION > 0)
1017 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
1019 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
1024 void wxTextCtrl::Undo()
1027 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1030 void wxTextCtrl::Redo()
1033 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1036 bool wxTextCtrl::CanUndo() const
1039 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1043 bool wxTextCtrl::CanRedo() const
1046 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1050 // If the return values from and to are the same, there is no
1052 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1054 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1057 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
1060 to
= GetInsertionPoint();
1062 else // got selection
1064 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
1065 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
1069 // exchange them to be compatible with wxMSW
1082 bool wxTextCtrl::IsEditable() const
1084 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1086 return GTK_EDITABLE(m_text
)->editable
;
1089 bool wxTextCtrl::IsModified() const
1094 void wxTextCtrl::Clear()
1096 SetValue( wxT("") );
1099 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1101 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1103 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1105 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1106 event
.SetEventObject(this);
1107 event
.SetString(GetValue());
1108 if (GetEventHandler()->ProcessEvent(event
)) return;
1111 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1113 wxWindow
*top_frame
= m_parent
;
1114 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1115 top_frame
= top_frame
->GetParent();
1116 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1118 if (window
->default_widget
)
1120 gtk_widget_activate (window
->default_widget
);
1128 GtkWidget
* wxTextCtrl::GetConnectWidget()
1130 return GTK_WIDGET(m_text
);
1133 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1135 if (m_windowStyle
& wxTE_MULTILINE
)
1136 return (window
== GTK_TEXT(m_text
)->text_area
);
1138 return (window
== GTK_ENTRY(m_text
)->text_area
);
1141 // the font will change for subsequent text insertiongs
1142 bool wxTextCtrl::SetFont( const wxFont
&font
)
1144 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1146 if ( !wxWindowBase::SetFont(font
) )
1148 // font didn't change, nothing to do
1152 if ( m_windowStyle
& wxTE_MULTILINE
)
1154 m_updateFont
= TRUE
;
1156 m_defaultStyle
.SetFont(font
);
1158 ChangeFontGlobally();
1164 void wxTextCtrl::ChangeFontGlobally()
1166 // this method is very inefficient and hence should be called as rarely as
1168 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1169 _T("shouldn't be called for single line controls") );
1171 wxString value
= GetValue();
1172 if ( !value
.IsEmpty() )
1174 m_updateFont
= FALSE
;
1181 void wxTextCtrl::UpdateFontIfNeeded()
1184 ChangeFontGlobally();
1187 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1189 if ( !wxControl::SetForegroundColour(colour
) )
1192 // update default fg colour too
1193 m_defaultStyle
.SetTextColour(colour
);
1198 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1200 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1202 if ( !wxControl::SetBackgroundColour( colour
) )
1205 if (!m_widget
->window
)
1208 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1209 if (sysbg
.Red() == colour
.Red() &&
1210 sysbg
.Green() == colour
.Green() &&
1211 sysbg
.Blue() == colour
.Blue())
1213 return FALSE
; // FIXME or TRUE?
1216 if (!m_backgroundColour
.Ok())
1219 if (m_windowStyle
& wxTE_MULTILINE
)
1221 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1224 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1225 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1226 gdk_window_clear( window
);
1229 // change active background color too
1230 m_defaultStyle
.SetBackgroundColour( colour
);
1235 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
&style
)
1237 /* VERY dirty way to do that - removes the required text and re-adds it
1238 with styling (FIXME) */
1239 if ( m_windowStyle
& wxTE_MULTILINE
)
1241 if ( style
.IsDefault() )
1247 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1249 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1250 _T("invalid range in wxTextCtrl::SetStyle") );
1252 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1253 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1254 wxString
tmp(text
,*wxConvCurrent
);
1257 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1258 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1261 wxWX2MBbuf buf
= tmp
.mbc_str();
1262 const char *txt
= buf
;
1263 size_t txtlen
= strlen(buf
);
1265 const char *txt
= tmp
;
1266 size_t txtlen
= tmp
.length();
1269 GdkFont
*font
= style
.HasFont()
1270 ? style
.GetFont().GetInternalFont()
1273 GdkColor
*colFg
= style
.HasTextColour()
1274 ? style
.GetTextColour().GetColor()
1277 GdkColor
*colBg
= style
.HasBackgroundColour()
1278 ? style
.GetBackgroundColour().GetColor()
1281 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
1283 /* does not seem to help under GTK+ 1.2 !!!
1284 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1285 SetInsertionPoint( old_pos
);
1290 // cannot do this for GTK+'s Entry widget
1295 void wxTextCtrl::ApplyWidgetStyle()
1297 if (m_windowStyle
& wxTE_MULTILINE
)
1304 gtk_widget_set_style( m_text
, m_widgetStyle
);
1308 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1313 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1318 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1323 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1328 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1333 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1335 event
.Enable( CanCut() );
1338 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1340 event
.Enable( CanCopy() );
1343 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1345 event
.Enable( CanPaste() );
1348 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1350 event
.Enable( CanUndo() );
1353 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1355 event
.Enable( CanRedo() );
1358 void wxTextCtrl::OnInternalIdle()
1360 wxCursor cursor
= m_cursor
;
1361 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1365 GdkWindow
*window
= (GdkWindow
*) NULL
;
1366 if (HasFlag(wxTE_MULTILINE
))
1367 window
= GTK_TEXT(m_text
)->text_area
;
1369 window
= GTK_ENTRY(m_text
)->text_area
;
1372 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1374 if (!g_globalCursor
.Ok())
1375 cursor
= *wxSTANDARD_CURSOR
;
1377 window
= m_widget
->window
;
1378 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1379 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1385 wxSize
wxTextCtrl::DoGetBestSize() const
1387 // FIXME should be different for multi-line controls...
1388 wxSize
ret( wxControl::DoGetBestSize() );
1389 return wxSize(80, ret
.y
);
1392 // ----------------------------------------------------------------------------
1394 // ----------------------------------------------------------------------------
1396 void wxTextCtrl::Freeze()
1398 if ( HasFlag(wxTE_MULTILINE
) )
1400 gtk_text_freeze(GTK_TEXT(m_text
));
1404 void wxTextCtrl::Thaw()
1406 if ( HasFlag(wxTE_MULTILINE
) )
1408 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1410 gtk_text_thaw(GTK_TEXT(m_text
));
1414 // ----------------------------------------------------------------------------
1416 // ----------------------------------------------------------------------------
1418 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1420 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1423 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1425 float value
= adj
->value
+ diff
;
1430 float upper
= adj
->upper
- adj
->page_size
;
1431 if ( value
> upper
)
1434 // did we noticeably change the scroll position?
1435 if ( fabs(adj
->value
- value
) < 0.2 )
1437 // well, this is what Robert does in wxScrollBar, so it must be good...
1443 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1448 bool wxTextCtrl::ScrollLines(int lines
)
1450 GtkAdjustment
*adj
= GetVAdj();
1454 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1455 static const int KEY_SCROLL_PIXELS
= 10;
1457 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1460 bool wxTextCtrl::ScrollPages(int pages
)
1462 GtkAdjustment
*adj
= GetVAdj();
1466 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));