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 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
49 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
51 if (!win
->m_hasVMT
) return;
54 wxapp_install_idle_handler();
57 win
->UpdateFontIfNeeded();
59 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
60 event
.SetString( win
->GetValue() );
61 event
.SetEventObject( win
);
62 win
->GetEventHandler()->ProcessEvent( event
);
65 //-----------------------------------------------------------------------------
66 // "changed" from vertical scrollbar
67 //-----------------------------------------------------------------------------
70 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
72 if (!win
->m_hasVMT
) return;
75 wxapp_install_idle_handler();
77 win
->CalculateScrollbar();
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 extern wxWindow
*g_focusWindow
;
85 extern bool g_blockEventsOnDrag
;
86 // extern bool g_isIdle;
88 static gint
gtk_text_focus_in_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
93 wxapp_install_idle_handler();
95 if (!win
->m_hasVMT
) return FALSE
;
96 if (g_blockEventsOnDrag
) return FALSE
;
100 // notify the parent that we got the focus
101 wxChildFocusEvent
eventFocus(win
);
102 (void)win
->GetEventHandler()->ProcessEvent(eventFocus
);
106 gdk_im_begin(win
->m_ic
, win
->m_wxwindow
->window
);
111 // caret needs to be informed about focus change
112 wxCaret
*caret
= win
->GetCaret();
117 #endif // wxUSE_CARET
120 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
121 event
.SetEventObject( win
);
123 if (win
->GetEventHandler()->ProcessEvent( event
))
131 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
135 static gint
gtk_text_focus_out_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
139 wxapp_install_idle_handler();
142 if (!win
->m_hasVMT
) return FALSE
;
143 if (g_blockEventsOnDrag
) return FALSE
;
146 // if the focus goes out of our app alltogether, OnIdle() will send
147 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
148 // g_sendActivateEvent to -1
149 g_sendActivateEvent
= 0;
152 wxWindow
*winFocus
= wxFindFocusedChild(win
);
156 g_focusWindow
= (wxWindow
*)NULL
;
165 // caret needs to be informed about focus change
166 wxCaret
*caret
= win
->GetCaret();
169 caret
->OnKillFocus();
171 #endif // wxUSE_CARET
174 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
175 event
.SetEventObject( win
);
177 if (win
->GetEventHandler()->ProcessEvent( event
))
185 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
189 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
191 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
192 EVT_CHAR(wxTextCtrl::OnChar
)
194 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
195 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
196 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
197 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
198 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
200 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
201 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
202 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
203 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
204 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
207 void wxTextCtrl::Init()
210 m_updateFont
= FALSE
;
212 m_vScrollbar
= (GtkWidget
*)NULL
;
215 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
217 const wxString
&value
,
221 const wxValidator
& validator
,
222 const wxString
&name
)
226 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
229 bool wxTextCtrl::Create( wxWindow
*parent
,
231 const wxString
&value
,
235 const wxValidator
& validator
,
236 const wxString
&name
)
239 m_acceptsFocus
= TRUE
;
241 if (!PreCreation( parent
, pos
, size
) ||
242 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
244 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
249 m_vScrollbarVisible
= FALSE
;
251 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
254 #if (GTK_MINOR_VERSION > 2)
255 /* a multi-line edit control: create a vertical scrollbar by default and
256 horizontal if requested */
257 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
259 bool bHasHScrollbar
= FALSE
;
262 /* create our control ... */
263 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
265 /* ... and put into the upper left hand corner of the table */
266 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
267 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
268 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
269 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
270 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
273 /* always wrap words */
274 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
276 #if (GTK_MINOR_VERSION > 2)
277 /* put the horizontal scrollbar in the lower left hand corner */
280 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
281 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
282 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
283 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
286 gtk_widget_show(hscrollbar
);
288 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
289 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
293 /* finally, put the vertical scrollbar in the upper right corner */
294 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
295 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
296 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
298 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
303 /* a single-line text control: no need for scrollbars */
305 m_text
= gtk_entry_new();
308 m_parent
->DoAddChild( this );
312 SetFont( parent
->GetFont() );
314 wxSize
size_best( DoGetBestSize() );
315 wxSize
new_size( size
);
316 if (new_size
.x
== -1)
317 new_size
.x
= size_best
.x
;
318 if (new_size
.y
== -1)
319 new_size
.y
= size_best
.y
;
320 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
321 SetSize( new_size
.x
, new_size
.y
);
324 gtk_widget_show(m_text
);
328 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
329 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
331 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_in_event",
332 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
334 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_out_event",
335 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
339 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_in_event",
340 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
342 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_out_event",
343 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
346 if (!value
.IsEmpty())
350 #if GTK_MINOR_VERSION == 0
351 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
352 // gtk_editable_insert_text()
353 gtk_widget_realize(m_text
);
357 wxWX2MBbuf val
= value
.mbc_str();
358 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
360 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
361 #endif // Unicode/!Unicode
365 /* bring editable's cursor uptodate. bug in GTK. */
367 GTK_EDITABLE(m_text
)->current_pos
= 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 // FIXME: is the bg colour correct here?
402 wxTextAttr
attrDef( colFg
,
403 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
),
405 SetDefaultStyle( attrDef
);
412 void wxTextCtrl::CalculateScrollbar()
414 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
416 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
418 if (adj
->upper
- adj
->page_size
< 0.8)
420 if (m_vScrollbarVisible
)
422 gtk_widget_hide( m_vScrollbar
);
423 m_vScrollbarVisible
= FALSE
;
428 if (!m_vScrollbarVisible
)
430 gtk_widget_show( m_vScrollbar
);
431 m_vScrollbarVisible
= TRUE
;
436 wxString
wxTextCtrl::GetValue() const
438 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
441 if (m_windowStyle
& wxTE_MULTILINE
)
443 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
444 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
445 tmp
= wxString(text
,*wxConvCurrent
);
450 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
455 void wxTextCtrl::SetValue( const wxString
&value
)
457 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
459 if (m_windowStyle
& wxTE_MULTILINE
)
461 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
462 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
465 wxWX2MBbuf tmpbuf
= value
.mbc_str();
466 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
468 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
473 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
476 // GRG, Jun/2000: Changed this after a lot of discussion in
477 // the lists. wxWindows 2.2 will have a set of flags to
478 // customize this behaviour.
479 SetInsertionPoint(0);
484 void wxTextCtrl::WriteText( const wxString
&text
)
486 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
492 wxWX2MBbuf buf
= text
.mbc_str();
493 const char *txt
= buf
;
494 size_t txtlen
= strlen(buf
);
496 const char *txt
= text
;
497 size_t txtlen
= text
.length();
500 if ( m_windowStyle
& wxTE_MULTILINE
)
502 /* this moves the cursor pos to behind the inserted text */
503 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
505 // if we have any special style, use it
506 if ( !m_defaultStyle
.IsDefault() )
508 GdkFont
*font
= m_defaultStyle
.HasFont()
509 ? m_defaultStyle
.GetFont().GetInternalFont()
512 GdkColor
*colFg
= m_defaultStyle
.HasTextColour()
513 ? m_defaultStyle
.GetTextColour().GetColor()
516 GdkColor
*colBg
= m_defaultStyle
.HasBackgroundColour()
517 ? m_defaultStyle
.GetBackgroundColour().GetColor()
520 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
524 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
527 /* bring editable's cursor uptodate. bug in GTK. */
528 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
532 /* this moves the cursor pos to behind the inserted text */
533 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
534 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
536 /* bring editable's cursor uptodate. bug in GTK. */
537 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
539 /* bring entry's cursor uptodate. bug in GTK. */
540 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
544 void wxTextCtrl::AppendText( const wxString
&text
)
546 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
552 wxWX2MBbuf buf
= text
.mbc_str();
553 const char *txt
= buf
;
554 size_t txtlen
= strlen(buf
);
556 const char *txt
= text
;
557 size_t txtlen
= text
.length();
560 if (m_windowStyle
& wxTE_MULTILINE
)
562 if ( !m_defaultStyle
.IsDefault() )
564 wxFont font
= m_defaultStyle
.HasFont() ? m_defaultStyle
.GetFont()
566 GdkFont
*fnt
= font
.Ok() ? font
.GetInternalFont() : NULL
;
568 wxColour col
= m_defaultStyle
.HasTextColour()
569 ? m_defaultStyle
.GetTextColour()
570 : m_foregroundColour
;
571 GdkColor
*colFg
= col
.Ok() ? col
.GetColor() : NULL
;
573 col
= m_defaultStyle
.HasBackgroundColour()
574 ? m_defaultStyle
.GetBackgroundColour()
575 : m_backgroundColour
;
576 GdkColor
*colBg
= col
.Ok() ? col
.GetColor() : NULL
;
578 gtk_text_insert( GTK_TEXT(m_text
), fnt
, colFg
, colBg
, txt
, txtlen
);
582 /* we'll insert at the last position */
583 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
584 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
587 /* bring editable's cursor uptodate. bug in GTK. */
588 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
592 gtk_entry_append_text( GTK_ENTRY(m_text
), txt
);
596 wxString
wxTextCtrl::GetLineText( long lineNo
) const
598 if (m_windowStyle
& wxTE_MULTILINE
)
600 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
601 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
605 wxString
buf(wxT(""));
608 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
613 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
620 return wxEmptyString
;
624 if (lineNo
== 0) return GetValue();
625 return wxEmptyString
;
629 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
631 /* If you implement this, don't forget to update the documentation!
632 * (file docs/latex/wx/text.tex) */
633 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
636 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
638 if ( m_windowStyle
& wxTE_MULTILINE
)
640 wxString text
= GetValue();
642 // cast to prevent warning. But pos really should've been unsigned.
643 if( (unsigned long)pos
> text
.Len() )
649 const wxChar
* stop
= text
.c_str() + pos
;
650 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
661 else // single line control
663 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
670 // index out of bounds
678 long wxTextCtrl::XYToPosition(long x
, long y
) const
680 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
683 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
689 int wxTextCtrl::GetLineLength(long lineNo
) const
691 wxString str
= GetLineText (lineNo
);
692 return (int) str
.Length();
695 int wxTextCtrl::GetNumberOfLines() const
697 if (m_windowStyle
& wxTE_MULTILINE
)
699 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
700 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
705 for (int i
= 0; i
< len
; i
++ )
712 // currentLine is 0 based, add 1 to get number of lines
713 return currentLine
+ 1;
726 void wxTextCtrl::SetInsertionPoint( long pos
)
728 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
730 if (m_windowStyle
& wxTE_MULTILINE
)
732 /* seems to be broken in GTK 1.0.X:
733 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
735 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
736 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
738 /* we fake a set_point by inserting and deleting. as the user
739 isn't supposed to get to know about thos non-sense, we
740 disconnect so that no events are sent to the user program. */
742 gint tmp
= (gint
)pos
;
743 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
744 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
746 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
747 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
749 /* bring editable's cursor uptodate. another bug in GTK. */
751 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
755 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
757 /* bring editable's cursor uptodate. bug in GTK. */
759 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
763 void wxTextCtrl::SetInsertionPointEnd()
765 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
767 if (m_windowStyle
& wxTE_MULTILINE
)
768 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
770 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
773 void wxTextCtrl::SetEditable( bool editable
)
775 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
777 if (m_windowStyle
& wxTE_MULTILINE
)
778 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
780 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
783 bool wxTextCtrl::Enable( bool enable
)
785 if (!wxWindowBase::Enable(enable
))
791 if (m_windowStyle
& wxTE_MULTILINE
)
793 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
794 OnParentEnable(enable
);
798 gtk_widget_set_sensitive( m_text
, enable
);
804 // wxGTK-specific: called recursively by Enable,
805 // to give widgets an oppprtunity to correct their colours after they
806 // have been changed by Enable
807 void wxTextCtrl::OnParentEnable( bool enable
)
809 // If we have a custom background colour, we use this colour in both
810 // disabled and enabled mode, or we end up with a different colour under the
812 wxColour oldColour
= GetBackgroundColour();
815 // Need to set twice or it'll optimize the useful stuff out
816 if (oldColour
== * wxWHITE
)
817 SetBackgroundColour(*wxBLACK
);
819 SetBackgroundColour(*wxWHITE
);
820 SetBackgroundColour(oldColour
);
824 void wxTextCtrl::DiscardEdits()
829 void wxTextCtrl::SetMaxLength(unsigned long len
)
831 if ( !HasFlag(wxTE_MULTILINE
) )
833 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
837 void wxTextCtrl::SetSelection( long from
, long to
)
839 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
841 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
842 !GTK_TEXT(m_text
)->line_start_cache
)
844 // tell the programmer that it didn't work
845 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
849 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
852 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
854 // SetInsertionPoint( pos );
857 long wxTextCtrl::GetInsertionPoint() const
859 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
861 return (long) GTK_EDITABLE(m_text
)->current_pos
;
864 long wxTextCtrl::GetLastPosition() const
866 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
869 if (m_windowStyle
& wxTE_MULTILINE
)
870 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
872 pos
= GTK_ENTRY(m_text
)->text_length
;
877 void wxTextCtrl::Remove( long from
, long to
)
879 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
881 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
884 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
886 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
888 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
890 if (!value
.IsEmpty())
892 gint pos
= (gint
)from
;
894 wxWX2MBbuf buf
= value
.mbc_str();
895 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
897 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
902 void wxTextCtrl::Cut()
904 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
906 #if (GTK_MINOR_VERSION > 0)
907 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
909 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
913 void wxTextCtrl::Copy()
915 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
917 #if (GTK_MINOR_VERSION > 0)
918 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
920 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
924 void wxTextCtrl::Paste()
926 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
928 #if (GTK_MINOR_VERSION > 0)
929 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
931 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
936 void wxTextCtrl::Undo()
939 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
942 void wxTextCtrl::Redo()
945 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
948 bool wxTextCtrl::CanUndo() const
951 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
955 bool wxTextCtrl::CanRedo() const
958 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
962 // If the return values from and to are the same, there is no
964 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
966 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
969 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
972 to
= GetInsertionPoint();
974 else // got selection
976 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
977 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
981 // exchange them to be compatible with wxMSW
994 bool wxTextCtrl::IsEditable() const
996 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
998 return GTK_EDITABLE(m_text
)->editable
;
1001 bool wxTextCtrl::IsModified() const
1006 void wxTextCtrl::Clear()
1008 SetValue( wxT("") );
1011 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1013 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1015 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1017 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1018 event
.SetEventObject(this);
1019 event
.SetString(GetValue());
1020 if (GetEventHandler()->ProcessEvent(event
)) return;
1023 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1025 wxWindow
*top_frame
= m_parent
;
1026 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1027 top_frame
= top_frame
->GetParent();
1028 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1030 if (window
->default_widget
)
1032 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 ( !wxWindowBase::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::GetSystemColour( 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 GdkFont
*font
= style
.HasFont()
1182 ? style
.GetFont().GetInternalFont()
1185 GdkColor
*colFg
= style
.HasTextColour()
1186 ? style
.GetTextColour().GetColor()
1189 GdkColor
*colBg
= style
.HasBackgroundColour()
1190 ? style
.GetBackgroundColour().GetColor()
1193 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
1195 /* does not seem to help under GTK+ 1.2 !!!
1196 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1197 SetInsertionPoint( old_pos
);
1202 // cannot do this for GTK+'s Entry widget
1207 void wxTextCtrl::ApplyWidgetStyle()
1209 if (m_windowStyle
& wxTE_MULTILINE
)
1216 gtk_widget_set_style( m_text
, m_widgetStyle
);
1220 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1225 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1230 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1235 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1240 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1245 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1247 event
.Enable( CanCut() );
1250 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1252 event
.Enable( CanCopy() );
1255 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1257 event
.Enable( CanPaste() );
1260 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1262 event
.Enable( CanUndo() );
1265 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1267 event
.Enable( CanRedo() );
1270 void wxTextCtrl::OnInternalIdle()
1272 wxCursor cursor
= m_cursor
;
1273 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1277 GdkWindow
*window
= (GdkWindow
*) NULL
;
1278 if (HasFlag(wxTE_MULTILINE
))
1279 window
= GTK_TEXT(m_text
)->text_area
;
1281 window
= GTK_ENTRY(m_text
)->text_area
;
1284 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1286 if (!g_globalCursor
.Ok())
1287 cursor
= *wxSTANDARD_CURSOR
;
1289 window
= m_widget
->window
;
1290 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1291 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1297 wxSize
wxTextCtrl::DoGetBestSize() const
1299 // FIXME should be different for multi-line controls...
1300 wxSize
ret( wxControl::DoGetBestSize() );
1301 return wxSize(80, ret
.y
);
1304 // ----------------------------------------------------------------------------
1306 // ----------------------------------------------------------------------------
1308 void wxTextCtrl::Freeze()
1310 if ( HasFlag(wxTE_MULTILINE
) )
1312 gtk_text_freeze(GTK_TEXT(m_text
));
1316 void wxTextCtrl::Thaw()
1318 if ( HasFlag(wxTE_MULTILINE
) )
1320 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1322 gtk_text_thaw(GTK_TEXT(m_text
));
1326 // ----------------------------------------------------------------------------
1328 // ----------------------------------------------------------------------------
1330 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1332 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1335 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1337 float value
= adj
->value
+ diff
;
1342 float upper
= adj
->upper
- adj
->page_size
;
1343 if ( value
> upper
)
1346 // did we noticeably change the scroll position?
1347 if ( fabs(adj
->value
- value
) < 0.2 )
1349 // well, this is what Robert does in wxScrollBar, so it must be good...
1355 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1360 bool wxTextCtrl::ScrollLines(int lines
)
1362 GtkAdjustment
*adj
= GetVAdj();
1366 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1367 static const int KEY_SCROLL_PIXELS
= 10;
1369 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1372 bool wxTextCtrl::ScrollPages(int pages
)
1374 GtkAdjustment
*adj
= GetVAdj();
1378 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));