1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textctrl.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
20 #include "wx/settings.h"
24 #include "wx/strconv.h"
25 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
27 #include <sys/types.h>
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern wxWindowGTK
*g_delayedFocus
;
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
45 static void wxGtkOnRemoveTag(GtkTextBuffer
*buffer
,
52 g_object_get (tag
, "name", &name
, NULL
);
54 if (!name
|| strncmp(name
, prefix
, strlen(prefix
)))
55 // anonymous tag or not starting with prefix - don't remove
56 g_signal_stop_emission_by_name (buffer
, "remove_tag");
63 static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer
*text_buffer
,
64 const wxTextAttr
& attr
,
68 static gchar buf
[1024];
71 gulong remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
72 G_CALLBACK (wxGtkOnRemoveTag
), gpointer("WX"));
73 gtk_text_buffer_remove_all_tags(text_buffer
, start
, end
);
74 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
79 PangoFontDescription
*font_description
= attr
.GetFont().GetNativeFontInfo()->description
;
80 font_string
= pango_font_description_to_string(font_description
);
81 g_snprintf(buf
, sizeof(buf
), "WXFONT %s", font_string
);
82 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
85 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
86 "font-desc", font_description
,
88 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
91 if (attr
.GetFont().GetUnderlined())
93 g_snprintf(buf
, sizeof(buf
), "WXFONTUNDERLINE");
94 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
97 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
98 "underline-set", TRUE
,
99 "underline", PANGO_UNDERLINE_SINGLE
,
101 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
105 if (attr
.HasTextColour())
107 const GdkColor
*colFg
= attr
.GetTextColour().GetColor();
108 g_snprintf(buf
, sizeof(buf
), "WXFORECOLOR %d %d %d",
109 colFg
->red
, colFg
->green
, colFg
->blue
);
110 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
113 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
114 "foreground-gdk", colFg
, NULL
);
115 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
118 if (attr
.HasBackgroundColour())
120 const GdkColor
*colBg
= attr
.GetBackgroundColour().GetColor();
121 g_snprintf(buf
, sizeof(buf
), "WXBACKCOLOR %d %d %d",
122 colBg
->red
, colBg
->green
, colBg
->blue
);
123 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
126 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
127 "background-gdk", colBg
, NULL
);
128 gtk_text_buffer_apply_tag (text_buffer
, tag
, start
, end
);
131 if (attr
.HasAlignment())
133 GtkTextIter para_start
, para_end
= *end
;
134 gtk_text_buffer_get_iter_at_line( text_buffer
,
136 gtk_text_iter_get_line(start
) );
137 gtk_text_iter_forward_line(¶_end
);
139 remove_handler_id
= g_signal_connect (text_buffer
, "remove_tag",
140 G_CALLBACK(wxGtkOnRemoveTag
),
141 gpointer("WXALIGNMENT"));
142 gtk_text_buffer_remove_all_tags( text_buffer
, ¶_start
, ¶_end
);
143 g_signal_handler_disconnect (text_buffer
, remove_handler_id
);
145 GtkJustification align
;
146 switch (attr
.GetAlignment())
149 align
= GTK_JUSTIFY_LEFT
;
151 case wxTEXT_ALIGNMENT_RIGHT
:
152 align
= GTK_JUSTIFY_RIGHT
;
154 case wxTEXT_ALIGNMENT_CENTER
:
155 align
= GTK_JUSTIFY_CENTER
;
157 // gtk+ doesn't support justify as of gtk+-2.7.4
160 g_snprintf(buf
, sizeof(buf
), "WXALIGNMENT %d", align
);
161 tag
= gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer
),
164 tag
= gtk_text_buffer_create_tag( text_buffer
, buf
,
165 "justification", align
, NULL
);
166 gtk_text_buffer_apply_tag( text_buffer
, tag
, ¶_start
, ¶_end
);
172 static void wxGtkTextInsert(GtkWidget
*text
,
173 GtkTextBuffer
*text_buffer
,
174 const wxTextAttr
& attr
,
175 const wxCharBuffer
& buffer
)
179 GtkTextIter iter
, start
;
181 gtk_text_buffer_get_iter_at_mark( text_buffer
, &iter
,
182 gtk_text_buffer_get_insert (text_buffer
) );
183 start_offset
= gtk_text_iter_get_offset (&iter
);
184 gtk_text_buffer_insert( text_buffer
, &iter
, buffer
, strlen(buffer
) );
186 gtk_text_buffer_get_iter_at_offset (text_buffer
, &start
, start_offset
);
188 wxGtkTextApplyTagsFromAttr(text_buffer
, attr
, &start
, &iter
);
192 // ----------------------------------------------------------------------------
193 // "insert_text" for GtkEntry
194 // ----------------------------------------------------------------------------
198 gtk_insert_text_callback(GtkEditable
*editable
,
199 const gchar
*new_text
,
200 gint new_text_length
,
205 wxapp_install_idle_handler();
207 // we should only be called if we have a max len limit at all
208 GtkEntry
*entry
= GTK_ENTRY (editable
);
210 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
212 // check that we don't overflow the max length limit
214 // FIXME: this doesn't work when we paste a string which is going to be
216 if ( entry
->text_length
== entry
->text_max_length
)
218 // we don't need to run the base class version at all
219 g_signal_stop_emission_by_name (editable
, "insert_text");
221 // remember that the next changed signal is to be ignored to avoid
222 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
223 win
->IgnoreNextTextUpdate();
225 // and generate the correct one ourselves
226 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
227 event
.SetEventObject(win
);
228 event
.SetString(win
->GetValue());
229 win
->GetEventHandler()->ProcessEvent( event
);
234 // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp,
238 au_apply_tag_callback(GtkTextBuffer
*buffer
,
244 if(tag
== gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl"))
245 g_signal_stop_emission_by_name (buffer
, "apply_tag");
249 //-----------------------------------------------------------------------------
250 // GtkTextCharPredicates for gtk_text_iter_*_find_char
251 //-----------------------------------------------------------------------------
255 pred_whitespace (gunichar ch
, gpointer user_data
)
257 return g_unichar_isspace(ch
);
263 pred_non_whitespace (gunichar ch
, gpointer user_data
)
265 return !g_unichar_isspace(ch
);
271 pred_nonpunct (gunichar ch
, gpointer user_data
)
273 return !g_unichar_ispunct(ch
);
279 pred_nonpunct_or_slash (gunichar ch
, gpointer user_data
)
281 return !g_unichar_ispunct(ch
) || ch
== '/';
285 //-----------------------------------------------------------------------------
286 // Check for links between s and e and correct tags as necessary
287 //-----------------------------------------------------------------------------
289 // This function should be made match better while being efficient at one point.
290 // Most probably with a row of regular expressions.
293 au_check_word( GtkTextIter
*s
, GtkTextIter
*e
)
295 static const char *URIPrefixes
[] =
313 GtkTextIter start
= *s
, end
= *e
;
314 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
316 // Get our special link tag
317 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
319 // Get rid of punctuation from beginning and end.
320 // Might want to move this to au_check_range if an improved link checking doesn't
321 // use some intelligent punctuation checking itself (beware of undesired iter modifications).
322 if(g_unichar_ispunct( gtk_text_iter_get_char( &start
) ) )
323 gtk_text_iter_forward_find_char( &start
, pred_nonpunct
, NULL
, e
);
325 gtk_text_iter_backward_find_char( &end
, pred_nonpunct_or_slash
, NULL
, &start
);
326 gtk_text_iter_forward_char(&end
);
328 gchar
* text
= gtk_text_iter_get_text( &start
, &end
);
329 size_t len
= strlen(text
), prefix_len
;
332 for( n
= 0; n
< WXSIZEOF(URIPrefixes
); ++n
)
334 prefix_len
= strlen(URIPrefixes
[n
]);
335 if((len
> prefix_len
) && !strncasecmp(text
, URIPrefixes
[n
], prefix_len
))
339 if(n
< WXSIZEOF(URIPrefixes
))
341 gulong signal_id
= g_signal_handler_find (buffer
,
342 (GSignalMatchType
) (G_SIGNAL_MATCH_FUNC
),
344 (gpointer
)au_apply_tag_callback
, NULL
);
346 g_signal_handler_block (buffer
, signal_id
);
347 gtk_text_buffer_apply_tag(buffer
, tag
, &start
, &end
);
348 g_signal_handler_unblock (buffer
, signal_id
);
355 au_check_range(GtkTextIter
*s
,
356 GtkTextIter
*range_end
)
358 GtkTextIter range_start
= *s
;
359 GtkTextIter word_end
;
360 GtkTextBuffer
*buffer
= gtk_text_iter_get_buffer(s
);
361 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer
), "wxUrl");
363 gtk_text_buffer_remove_tag(buffer
, tag
, s
, range_end
);
365 if(g_unichar_isspace(gtk_text_iter_get_char(&range_start
)))
366 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
368 while(!gtk_text_iter_equal(&range_start
, range_end
))
370 word_end
= range_start
;
371 gtk_text_iter_forward_find_char(&word_end
, pred_whitespace
, NULL
, range_end
);
373 // Now we should have a word delimited by range_start and word_end, correct link tags
374 au_check_word(&range_start
, &word_end
);
376 range_start
= word_end
;
377 gtk_text_iter_forward_find_char(&range_start
, pred_non_whitespace
, NULL
, range_end
);
382 //-----------------------------------------------------------------------------
383 // "insert-text" for GtkTextBuffer
384 //-----------------------------------------------------------------------------
388 au_insert_text_callback(GtkTextBuffer
*buffer
,
394 if (!len
|| !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
397 GtkTextIter start
= *end
;
398 gtk_text_iter_backward_chars(&start
, g_utf8_strlen(text
, len
));
400 GtkTextIter line_start
= start
;
401 GtkTextIter line_end
= *end
;
402 GtkTextIter words_start
= start
;
403 GtkTextIter words_end
= *end
;
405 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(&start
));
406 gtk_text_iter_forward_to_line_end(&line_end
);
407 gtk_text_iter_backward_find_char(&words_start
, pred_whitespace
, NULL
, &line_start
);
408 gtk_text_iter_forward_find_char(&words_end
, pred_whitespace
, NULL
, &line_end
);
410 au_check_range(&words_start
, &words_end
);
414 //-----------------------------------------------------------------------------
415 // "delete-range" for GtkTextBuffer
416 //-----------------------------------------------------------------------------
420 au_delete_range_callback(GtkTextBuffer
*buffer
,
425 if( !(win
->GetWindowStyleFlag() & wxTE_AUTO_URL
) )
428 GtkTextIter line_start
= *start
, line_end
= *end
;
430 gtk_text_iter_set_line(&line_start
, gtk_text_iter_get_line(start
));
431 gtk_text_iter_forward_to_line_end(&line_end
);
432 gtk_text_iter_backward_find_char(start
, pred_whitespace
, NULL
, &line_start
);
433 gtk_text_iter_forward_find_char(end
, pred_whitespace
, NULL
, &line_end
);
435 au_check_range(start
, end
);
440 //-----------------------------------------------------------------------------
442 //-----------------------------------------------------------------------------
446 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
448 if ( win
->IgnoreTextUpdate() )
451 if (!win
->m_hasVMT
) return;
454 wxapp_install_idle_handler();
456 if ( win
->MarkDirtyOnChange() )
459 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
460 event
.SetEventObject( win
);
461 win
->GetEventHandler()->ProcessEvent( event
);
465 //-----------------------------------------------------------------------------
466 // clipboard events: "copy-clipboard", "cut-clipboard", "paste-clipboard"
467 //-----------------------------------------------------------------------------
469 // common part of the event handlers below
471 handle_text_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
,
472 wxEventType eventType
, const gchar
* signal_name
)
474 wxClipboardTextEvent
event( eventType
, win
->GetId() );
475 event
.SetEventObject( win
);
476 if ( win
->GetEventHandler()->ProcessEvent( event
) )
478 // don't let the default processing to take place if we did something
479 // ourselves in the event handler
480 g_signal_stop_emission_by_name (widget
, signal_name
);
486 gtk_copy_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
488 handle_text_clipboard_callback(
489 widget
, win
, wxEVT_COMMAND_TEXT_COPY
, "copy-clipboard" );
493 gtk_cut_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
495 handle_text_clipboard_callback(
496 widget
, win
, wxEVT_COMMAND_TEXT_CUT
, "cut-clipboard" );
500 gtk_paste_clipboard_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
502 handle_text_clipboard_callback(
503 widget
, win
, wxEVT_COMMAND_TEXT_PASTE
, "paste-clipboard" );
507 //-----------------------------------------------------------------------------
508 // "expose_event" from scrolled window and textview
509 //-----------------------------------------------------------------------------
513 gtk_text_exposed_callback( GtkWidget
*widget
, GdkEventExpose
*event
, wxTextCtrl
*win
)
520 //-----------------------------------------------------------------------------
522 //-----------------------------------------------------------------------------
524 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
526 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
527 EVT_CHAR(wxTextCtrl::OnChar
)
529 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
530 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
531 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
532 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
533 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
535 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
536 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
537 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
538 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
539 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
541 // wxTE_AUTO_URL wxTextUrl support. Currently only creates
542 // wxTextUrlEvent in the same cases as wxMSW, more can be added here.
543 EVT_MOTION (wxTextCtrl::OnUrlMouseEvent
)
544 EVT_LEFT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
545 EVT_LEFT_UP (wxTextCtrl::OnUrlMouseEvent
)
546 EVT_LEFT_DCLICK (wxTextCtrl::OnUrlMouseEvent
)
547 EVT_RIGHT_DOWN (wxTextCtrl::OnUrlMouseEvent
)
548 EVT_RIGHT_UP (wxTextCtrl::OnUrlMouseEvent
)
549 EVT_RIGHT_DCLICK(wxTextCtrl::OnUrlMouseEvent
)
552 void wxTextCtrl::Init()
558 SetUpdateFont(false);
562 m_gdkHandCursor
= NULL
;
563 m_gdkXTermCursor
= NULL
;
566 wxTextCtrl::~wxTextCtrl()
569 gdk_cursor_unref(m_gdkHandCursor
);
571 gdk_cursor_unref(m_gdkXTermCursor
);
574 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
576 const wxString
&value
,
580 const wxValidator
& validator
,
581 const wxString
&name
)
585 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
588 bool wxTextCtrl::Create( wxWindow
*parent
,
590 const wxString
&value
,
594 const wxValidator
& validator
,
595 const wxString
&name
)
598 m_acceptsFocus
= true;
600 if (!PreCreation( parent
, pos
, size
) ||
601 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
603 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
607 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
612 m_text
= gtk_text_view_new();
614 m_buffer
= gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text
) );
616 // create scrolled window
617 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
618 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget
),
619 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
620 // for ScrollLines/Pages
621 m_scrollBar
[1] = (GtkRange
*)((GtkScrolledWindow
*)m_widget
)->vscrollbar
;
623 // Insert view into scrolled window
624 gtk_container_add( GTK_CONTAINER(m_widget
), m_text
);
626 // translate wx wrapping style to GTK+
628 if ( HasFlag( wxTE_DONTWRAP
) )
629 wrap
= GTK_WRAP_NONE
;
630 else if ( HasFlag( wxTE_CHARWRAP
) )
631 wrap
= GTK_WRAP_CHAR
;
632 else if ( HasFlag( wxTE_WORDWRAP
) )
633 wrap
= GTK_WRAP_WORD
;
634 else // HasFlag(wxTE_BESTWRAP) always true as wxTE_BESTWRAP == 0
636 // GTK_WRAP_WORD_CHAR seems to be new in GTK+ 2.4
638 if ( !gtk_check_version(2,4,0) )
640 wrap
= GTK_WRAP_WORD_CHAR
;
644 wrap
= GTK_WRAP_WORD
;
647 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text
), wrap
);
649 GtkScrolledWindowSetBorder(m_widget
, style
);
651 gtk_widget_add_events( GTK_WIDGET(m_text
), GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
);
653 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
657 // a single-line text control: no need for scrollbars
659 m_text
= gtk_entry_new();
661 if (style
& wxNO_BORDER
)
662 g_object_set (m_text
, "has-frame", FALSE
, NULL
);
665 m_parent
->DoAddChild( this );
667 m_focusWidget
= m_text
;
673 gtk_widget_show(m_text
);
681 if (style
& wxTE_PASSWORD
)
684 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
687 if (style
& wxTE_READONLY
)
690 gtk_editable_set_editable( GTK_EDITABLE(m_text
), FALSE
);
692 gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text
), FALSE
);
697 if (style
& wxTE_RIGHT
)
698 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_RIGHT
);
699 else if (style
& wxTE_CENTRE
)
700 gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text
), GTK_JUSTIFY_CENTER
);
701 // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT
706 // gtk_entry_set_alignment was introduced in gtk+-2.3.5
707 if (!gtk_check_version(2,4,0))
709 if (style
& wxTE_RIGHT
)
710 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 1.0 );
711 else if (style
& wxTE_CENTRE
)
712 gtk_entry_set_alignment( GTK_ENTRY(m_text
), 0.5 );
717 // We want to be notified about text changes.
720 g_signal_connect (m_buffer
, "changed",
721 G_CALLBACK (gtk_text_changed_callback
), this);
723 // .. and handle URLs on multi-line controls with wxTE_AUTO_URL style
724 if (style
& wxTE_AUTO_URL
)
726 GtkTextIter start
, end
;
727 m_gdkHandCursor
= gdk_cursor_new(GDK_HAND2
);
728 m_gdkXTermCursor
= gdk_cursor_new(GDK_XTERM
);
730 // We create our wxUrl tag here for slight efficiency gain - we
731 // don't have to check for the tag existance in callbacks,
732 // hereby it's guaranteed to exist.
733 gtk_text_buffer_create_tag(m_buffer
, "wxUrl",
734 "foreground", "blue",
735 "underline", PANGO_UNDERLINE_SINGLE
,
738 // Check for URLs after each text change
739 g_signal_connect_after (m_buffer
, "insert_text",
740 G_CALLBACK (au_insert_text_callback
), this);
741 g_signal_connect_after (m_buffer
, "delete_range",
742 G_CALLBACK (au_delete_range_callback
), this);
744 // Block all wxUrl tag applying unless we do it ourselves, in which case we
745 // block this callback temporarily. This takes care of gtk+ internal
746 // gtk_text_buffer_insert_range* calls that would copy our URL tag otherwise,
747 // which is undesired because only a part of the URL might be copied.
748 // The insert-text signal emitted inside it will take care of newly formed
749 // or wholly copied URLs.
750 g_signal_connect (m_buffer
, "apply_tag",
751 G_CALLBACK (au_apply_tag_callback
), NULL
);
753 // Check for URLs in the initial string passed to Create
754 gtk_text_buffer_get_start_iter(m_buffer
, &start
);
755 gtk_text_buffer_get_end_iter(m_buffer
, &end
);
756 au_check_range(&start
, &end
);
761 g_signal_connect (m_text
, "changed",
762 G_CALLBACK (gtk_text_changed_callback
), this);
765 g_signal_connect (m_text
, "copy-clipboard",
766 G_CALLBACK (gtk_copy_clipboard_callback
), this);
767 g_signal_connect (m_text
, "cut-clipboard",
768 G_CALLBACK (gtk_cut_clipboard_callback
), this);
769 g_signal_connect (m_text
, "paste-clipboard",
770 G_CALLBACK (gtk_paste_clipboard_callback
), this);
772 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
774 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
775 SetDefaultStyle( attrDef
);
781 void wxTextCtrl::CalculateScrollbar()
785 wxString
wxTextCtrl::GetValue() const
787 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
793 gtk_text_buffer_get_start_iter( m_buffer
, &start
);
795 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
796 gchar
*text
= gtk_text_buffer_get_text( m_buffer
, &start
, &end
, TRUE
);
798 const wxWxCharBuffer buf
= wxGTK_CONV_BACK(text
);
806 const gchar
*text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
807 const wxWxCharBuffer buf
= wxGTK_CONV_BACK( text
);
815 wxFontEncoding
wxTextCtrl::GetTextEncoding() const
817 // GTK+ uses UTF-8 internally, we need to convert to it but from which
820 // first check the default text style (we intentionally don't check the
821 // style for the current position as it doesn't make sense for SetValue())
822 const wxTextAttr
& style
= GetDefaultStyle();
823 wxFontEncoding enc
= style
.HasFont() ? style
.GetFont().GetEncoding()
824 : wxFONTENCODING_SYSTEM
;
826 // fall back to the controls font if no style
827 if ( enc
== wxFONTENCODING_SYSTEM
&& m_hasFont
)
828 enc
= GetFont().GetEncoding();
833 void wxTextCtrl::SetValue( const wxString
&value
)
835 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
837 // the control won't be modified any more as we programmatically replace
838 // all the existing text, so reset the flag and don't set it again (and do
839 // it now, before the text event handler is ran so that IsModified() called
840 // from there returns the expected value)
842 DontMarkDirtyOnNextChange();
846 const wxCharBuffer
buffer(wxGTK_CONV_ENC(value
, GetTextEncoding()));
849 // see comment in WriteText() as to why we must warn the user about
851 wxLogWarning(_("Failed to set text in the text control."));
855 if (gtk_text_buffer_get_char_count(m_buffer
) != 0)
856 IgnoreNextTextUpdate();
858 gtk_text_buffer_set_text( m_buffer
, buffer
, strlen(buffer
) );
862 // gtk_entry_set_text() emits two "changed" signals because internally
863 // it calls gtk_editable_delete_text() and gtk_editable_insert_text()
864 // but we want to have only one event
865 IgnoreNextTextUpdate();
867 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV(value
) );
870 // GRG, Jun/2000: Changed this after a lot of discussion in
871 // the lists. wxWidgets 2.2 will have a set of flags to
872 // customize this behaviour.
873 SetInsertionPoint(0);
876 void wxTextCtrl::WriteText( const wxString
&text
)
878 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
883 // check if we have a specific style for the current position
884 wxFontEncoding enc
= wxFONTENCODING_SYSTEM
;
886 if ( GetStyle(GetInsertionPoint(), style
) && style
.HasFont() )
888 enc
= style
.GetFont().GetEncoding();
891 if ( enc
== wxFONTENCODING_SYSTEM
)
892 enc
= GetTextEncoding();
894 const wxCharBuffer
buffer(wxGTK_CONV_ENC(text
, enc
));
897 // we must log an error here as losing the text like this can be a
898 // serious problem (e.g. imagine the document edited by user being
899 // empty instead of containing the correct text)
900 wxLogWarning(_("Failed to insert text in the control."));
904 // we're changing the text programmatically
905 DontMarkDirtyOnNextChange();
909 // First remove the selection if there is one
910 // TODO: Is there an easier GTK specific way to do this?
912 GetSelection(&from
, &to
);
917 wxGtkTextInsert( m_text
, m_buffer
, m_defaultStyle
, buffer
);
919 GtkAdjustment
*adj
= gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget
) );
920 // Scroll to cursor, but only if scrollbar thumb is at the very bottom
921 if ( wxIsSameDouble(adj
->value
, adj
->upper
- adj
->page_size
) )
923 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
),
924 gtk_text_buffer_get_insert( m_buffer
), 0.0, FALSE
, 0.0, 1.0 );
929 // First remove the selection if there is one
930 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
932 // This moves the cursor pos to behind the inserted text.
933 gint len
= gtk_editable_get_position(GTK_EDITABLE(m_text
));
935 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buffer
, strlen(buffer
), &len
);
937 // Bring entry's cursor uptodate.
938 gtk_editable_set_position( GTK_EDITABLE(m_text
), len
);
942 void wxTextCtrl::AppendText( const wxString
&text
)
944 SetInsertionPointEnd();
948 wxString
wxTextCtrl::GetLineText( long lineNo
) const
953 gtk_text_buffer_get_iter_at_line(m_buffer
,&line
,lineNo
);
954 GtkTextIter end
= line
;
955 gtk_text_iter_forward_to_line_end(&end
);
956 gchar
*text
= gtk_text_buffer_get_text(m_buffer
,&line
,&end
,TRUE
);
957 wxString
result(wxGTK_CONV_BACK(text
));
963 if (lineNo
== 0) return GetValue();
964 return wxEmptyString
;
968 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
970 /* If you implement this, don't forget to update the documentation!
971 * (file docs/latex/wx/text.tex) */
972 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
975 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
981 if (pos
> GetLastPosition())
984 gtk_text_buffer_get_iter_at_offset(m_buffer
, &iter
, pos
);
987 *y
= gtk_text_iter_get_line(&iter
);
989 *x
= gtk_text_iter_get_line_offset(&iter
);
991 else // single line control
993 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
1002 // index out of bounds
1010 long wxTextCtrl::XYToPosition(long x
, long y
) const
1012 if ( IsSingleLine() )
1016 if (y
>= gtk_text_buffer_get_line_count (m_buffer
))
1019 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, y
);
1020 if (x
>= gtk_text_iter_get_chars_in_line (&iter
))
1023 return gtk_text_iter_get_offset(&iter
) + x
;
1026 int wxTextCtrl::GetLineLength(long lineNo
) const
1028 if ( IsMultiLine() )
1030 int last_line
= gtk_text_buffer_get_line_count( m_buffer
) - 1;
1031 if (lineNo
> last_line
)
1035 gtk_text_buffer_get_iter_at_line(m_buffer
, &iter
, lineNo
);
1036 // get_chars_in_line return includes paragraph delimiters, so need to subtract 1 IF it is not the last line
1037 return gtk_text_iter_get_chars_in_line(&iter
) - ((lineNo
== last_line
) ? 0 : 1);
1041 wxString str
= GetLineText (lineNo
);
1042 return (int) str
.length();
1046 int wxTextCtrl::GetNumberOfLines() const
1048 if ( IsMultiLine() )
1051 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, 0 );
1053 // move forward by one display line until the end is reached
1055 while ( gtk_text_view_forward_display_line(GTK_TEXT_VIEW(m_text
), &iter
) )
1060 // If the last character in the text buffer is a newline,
1061 // gtk_text_view_forward_display_line() will return false without that
1062 // line being counted. Must add one manually in that case.
1063 GtkTextIter lastCharIter
;
1064 gtk_text_buffer_get_iter_at_offset
1068 gtk_text_buffer_get_char_count(m_buffer
) - 1
1070 gchar lastChar
= gtk_text_iter_get_char( &lastCharIter
);
1071 if ( lastChar
== wxT('\n') )
1082 void wxTextCtrl::SetInsertionPoint( long pos
)
1084 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1086 if ( IsMultiLine() )
1089 gtk_text_buffer_get_iter_at_offset( m_buffer
, &iter
, pos
);
1090 gtk_text_buffer_place_cursor( m_buffer
, &iter
);
1091 gtk_text_view_scroll_mark_onscreen
1093 GTK_TEXT_VIEW(m_text
),
1094 gtk_text_buffer_get_insert( m_buffer
)
1099 // FIXME: Is the editable's cursor really uptodate without double set_position in GTK2?
1100 gtk_editable_set_position(GTK_EDITABLE(m_text
), int(pos
));
1104 void wxTextCtrl::SetInsertionPointEnd()
1106 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1108 if ( IsMultiLine() )
1111 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1112 gtk_text_buffer_place_cursor( m_buffer
, &end
);
1116 gtk_editable_set_position( GTK_EDITABLE(m_text
), -1 );
1120 void wxTextCtrl::SetEditable( bool editable
)
1122 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1124 if ( IsMultiLine() )
1126 gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text
), editable
);
1130 gtk_editable_set_editable( GTK_EDITABLE(m_text
), editable
);
1134 bool wxTextCtrl::Enable( bool enable
)
1136 if (!wxWindowBase::Enable(enable
))
1142 if ( IsMultiLine() )
1144 SetEditable( enable
);
1148 gtk_widget_set_sensitive( m_text
, enable
);
1154 // wxGTK-specific: called recursively by Enable,
1155 // to give widgets an oppprtunity to correct their colours after they
1156 // have been changed by Enable
1157 void wxTextCtrl::OnParentEnable( bool enable
)
1159 // If we have a custom background colour, we use this colour in both
1160 // disabled and enabled mode, or we end up with a different colour under the
1162 wxColour oldColour
= GetBackgroundColour();
1165 // Need to set twice or it'll optimize the useful stuff out
1166 if (oldColour
== * wxWHITE
)
1167 SetBackgroundColour(*wxBLACK
);
1169 SetBackgroundColour(*wxWHITE
);
1170 SetBackgroundColour(oldColour
);
1174 void wxTextCtrl::MarkDirty()
1179 void wxTextCtrl::DiscardEdits()
1184 // ----------------------------------------------------------------------------
1185 // max text length support
1186 // ----------------------------------------------------------------------------
1188 bool wxTextCtrl::IgnoreTextUpdate()
1190 if ( m_ignoreNextUpdate
)
1192 m_ignoreNextUpdate
= false;
1200 bool wxTextCtrl::MarkDirtyOnChange()
1202 if ( m_dontMarkDirty
)
1204 m_dontMarkDirty
= false;
1212 void wxTextCtrl::SetMaxLength(unsigned long len
)
1214 if ( !HasFlag(wxTE_MULTILINE
) )
1216 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
1218 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
1219 // we had tried to enter more text than allowed by max text length and
1220 // the text wasn't really changed
1222 // to detect this and generate TEXT_MAXLEN event instead of
1223 // TEXT_CHANGED one in this case we also catch "insert_text" signal
1225 // when max len is set to 0 we disconnect our handler as it means that
1226 // we shouldn't check anything any more
1229 g_signal_connect (m_text
, "insert_text",
1230 G_CALLBACK (gtk_insert_text_callback
), this);
1234 g_signal_handlers_disconnect_by_func (m_text
,
1235 (gpointer
) gtk_insert_text_callback
, this);
1240 void wxTextCtrl::SetSelection( long from
, long to
)
1242 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1244 if (from
== -1 && to
== -1)
1247 to
= GetValue().length();
1250 if ( IsMultiLine() )
1252 GtkTextIter fromi
, toi
;
1253 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1254 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1256 gtk_text_buffer_place_cursor( m_buffer
, &toi
);
1257 gtk_text_buffer_move_mark_by_name( m_buffer
, "selection_bound", &fromi
);
1261 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1265 void wxTextCtrl::ShowPosition( long pos
)
1267 if ( IsMultiLine() )
1270 gtk_text_buffer_get_start_iter( m_buffer
, &iter
);
1271 gtk_text_iter_set_offset( &iter
, pos
);
1272 GtkTextMark
*mark
= gtk_text_buffer_create_mark( m_buffer
, NULL
, &iter
, TRUE
);
1273 gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text
), mark
, 0.0, FALSE
, 0.0, 0.0 );
1277 wxTextCtrlHitTestResult
1278 wxTextCtrl::HitTest(const wxPoint
& pt
, long *pos
) const
1280 if ( !IsMultiLine() )
1283 return wxTE_HT_UNKNOWN
;
1287 gtk_text_view_window_to_buffer_coords
1289 GTK_TEXT_VIEW(m_text
),
1290 GTK_TEXT_WINDOW_TEXT
,
1296 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &iter
, x
, y
);
1298 *pos
= gtk_text_iter_get_offset(&iter
);
1300 return wxTE_HT_ON_TEXT
;
1303 long wxTextCtrl::GetInsertionPoint() const
1305 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1307 if ( IsMultiLine() )
1309 // There is no direct accessor for the cursor, but
1310 // internally, the cursor is the "mark" called
1311 // "insert" in the text view's btree structure.
1313 GtkTextMark
*mark
= gtk_text_buffer_get_insert( m_buffer
);
1315 gtk_text_buffer_get_iter_at_mark( m_buffer
, &cursor
, mark
);
1317 return gtk_text_iter_get_offset( &cursor
);
1321 return (long) gtk_editable_get_position(GTK_EDITABLE(m_text
));
1325 wxTextPos
wxTextCtrl::GetLastPosition() const
1327 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
1331 if ( IsMultiLine() )
1334 gtk_text_buffer_get_end_iter( m_buffer
, &end
);
1336 pos
= gtk_text_iter_get_offset( &end
);
1340 pos
= GTK_ENTRY(m_text
)->text_length
;
1346 void wxTextCtrl::Remove( long from
, long to
)
1348 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1350 if ( IsMultiLine() )
1352 GtkTextIter fromi
, toi
;
1353 gtk_text_buffer_get_iter_at_offset( m_buffer
, &fromi
, from
);
1354 gtk_text_buffer_get_iter_at_offset( m_buffer
, &toi
, to
);
1356 gtk_text_buffer_delete( m_buffer
, &fromi
, &toi
);
1359 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
1362 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
1364 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1370 SetInsertionPoint( from
);
1375 void wxTextCtrl::Cut()
1377 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1379 if ( IsMultiLine() )
1380 g_signal_emit_by_name (m_text
, "cut-clipboard");
1382 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
));
1385 void wxTextCtrl::Copy()
1387 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1389 if ( IsMultiLine() )
1390 g_signal_emit_by_name (m_text
, "copy-clipboard");
1392 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
));
1395 void wxTextCtrl::Paste()
1397 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1399 if ( IsMultiLine() )
1400 g_signal_emit_by_name (m_text
, "paste-clipboard");
1402 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
));
1406 void wxTextCtrl::Undo()
1409 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
1412 void wxTextCtrl::Redo()
1415 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
1418 bool wxTextCtrl::CanUndo() const
1421 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
1425 bool wxTextCtrl::CanRedo() const
1428 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
1432 // If the return values from and to are the same, there is no
1434 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
1436 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1440 bool haveSelection
= false;
1442 if ( IsMultiLine() )
1444 GtkTextIter ifrom
, ito
;
1445 if ( gtk_text_buffer_get_selection_bounds(m_buffer
, &ifrom
, &ito
) )
1447 haveSelection
= true;
1448 from
= gtk_text_iter_get_offset(&ifrom
);
1449 to
= gtk_text_iter_get_offset(&ito
);
1452 else // not multi-line
1454 if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text
),
1457 haveSelection
= true;
1461 if (! haveSelection
)
1462 from
= to
= GetInsertionPoint();
1466 // exchange them to be compatible with wxMSW
1479 bool wxTextCtrl::IsEditable() const
1481 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1483 if ( IsMultiLine() )
1485 return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text
));
1489 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
1493 bool wxTextCtrl::IsModified() const
1498 void wxTextCtrl::Clear()
1500 SetValue( wxEmptyString
);
1503 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1505 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1507 if ( key_event
.GetKeyCode() == WXK_RETURN
)
1509 if ( HasFlag(wxTE_PROCESS_ENTER
) )
1511 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1512 event
.SetEventObject(this);
1513 event
.SetString(GetValue());
1514 if ( GetEventHandler()->ProcessEvent(event
) )
1518 // FIXME: this is not the right place to do it, wxDialog::OnCharHook()
1520 if ( IsSingleLine() )
1522 // This will invoke the dialog default action, such
1523 // as the clicking the default button.
1525 wxWindow
*top_frame
= m_parent
;
1526 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1527 top_frame
= top_frame
->GetParent();
1529 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1531 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1533 if (window
->default_widget
)
1535 gtk_widget_activate (window
->default_widget
);
1545 GtkWidget
* wxTextCtrl::GetConnectWidget()
1547 return GTK_WIDGET(m_text
);
1550 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1552 if ( IsMultiLine() )
1554 return window
== gtk_text_view_get_window( GTK_TEXT_VIEW( m_text
), GTK_TEXT_WINDOW_TEXT
); // pure guesswork
1558 return (window
== GTK_ENTRY(m_text
)->text_area
);
1562 // the font will change for subsequent text insertiongs
1563 bool wxTextCtrl::SetFont( const wxFont
&font
)
1565 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1567 if ( !wxTextCtrlBase::SetFont(font
) )
1569 // font didn't change, nothing to do
1573 if ( IsMultiLine() )
1575 SetUpdateFont(true);
1577 m_defaultStyle
.SetFont(font
);
1579 ChangeFontGlobally();
1585 void wxTextCtrl::ChangeFontGlobally()
1587 // this method is very inefficient and hence should be called as rarely as
1590 // TODO: it can be implemented much more efficiently for GTK2
1591 wxASSERT_MSG( IsMultiLine(),
1592 _T("shouldn't be called for single line controls") );
1594 wxString value
= GetValue();
1595 if ( !value
.empty() )
1597 SetUpdateFont(false);
1604 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1606 if ( !wxControl::SetForegroundColour(colour
) )
1609 // update default fg colour too
1610 m_defaultStyle
.SetTextColour(colour
);
1615 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1617 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1619 if ( !wxControl::SetBackgroundColour( colour
) )
1622 if (!m_backgroundColour
.Ok())
1625 // change active background color too
1626 m_defaultStyle
.SetBackgroundColour( colour
);
1631 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1633 if ( IsMultiLine() )
1635 if ( style
.IsDefault() )
1641 gint l
= gtk_text_buffer_get_char_count( m_buffer
);
1643 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1644 _T("invalid range in wxTextCtrl::SetStyle") );
1646 GtkTextIter starti
, endi
;
1647 gtk_text_buffer_get_iter_at_offset( m_buffer
, &starti
, start
);
1648 gtk_text_buffer_get_iter_at_offset( m_buffer
, &endi
, end
);
1650 // use the attributes from style which are set in it and fall back
1651 // first to the default style and then to the text control default
1652 // colours for the others
1653 wxTextAttr attr
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
1655 wxGtkTextApplyTagsFromAttr( m_buffer
, attr
, &starti
, &endi
);
1661 // cannot do this for GTK+'s Entry widget
1665 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1667 gtk_widget_modify_style(m_text
, style
);
1670 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1675 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1680 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1685 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1690 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1695 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1697 event
.Enable( CanCut() );
1700 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1702 event
.Enable( CanCopy() );
1705 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1707 event
.Enable( CanPaste() );
1710 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1712 event
.Enable( CanUndo() );
1715 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1717 event
.Enable( CanRedo() );
1720 void wxTextCtrl::OnInternalIdle()
1722 // Check if we have to show window now
1723 if (GtkShowFromOnIdle()) return;
1725 if (g_delayedFocus
== this)
1727 if (GTK_WIDGET_REALIZED(m_widget
))
1729 gtk_widget_grab_focus( m_widget
);
1730 g_delayedFocus
= NULL
;
1734 if (wxUpdateUIEvent::CanUpdate(this))
1735 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1738 wxSize
wxTextCtrl::DoGetBestSize() const
1740 // FIXME should be different for multi-line controls...
1741 wxSize
ret( wxControl::DoGetBestSize() );
1742 wxSize
best(80, ret
.y
);
1743 CacheBestSize(best
);
1747 // ----------------------------------------------------------------------------
1749 // ----------------------------------------------------------------------------
1751 void wxTextCtrl::Freeze()
1753 if ( HasFlag(wxTE_MULTILINE
) )
1755 if ( !m_frozenness
++ )
1757 // freeze textview updates and remove buffer
1758 g_signal_connect (m_text
, "expose_event",
1759 G_CALLBACK (gtk_text_exposed_callback
), this);
1760 g_signal_connect (m_widget
, "expose_event",
1761 G_CALLBACK (gtk_text_exposed_callback
), this);
1762 gtk_widget_set_sensitive(m_widget
, false);
1763 g_object_ref(m_buffer
);
1764 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), gtk_text_buffer_new(NULL
));
1769 void wxTextCtrl::Thaw()
1771 if ( HasFlag(wxTE_MULTILINE
) )
1773 wxASSERT_MSG( m_frozenness
> 0, _T("Thaw() without matching Freeze()") );
1775 if ( !--m_frozenness
)
1777 // Reattach buffer and thaw textview updates
1778 gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text
), m_buffer
);
1779 g_object_unref(m_buffer
);
1780 gtk_widget_set_sensitive(m_widget
, true);
1781 g_signal_handlers_disconnect_by_func (m_widget
,
1782 (gpointer
) gtk_text_exposed_callback
, this);
1783 g_signal_handlers_disconnect_by_func (m_text
,
1784 (gpointer
) gtk_text_exposed_callback
, this);
1789 // ----------------------------------------------------------------------------
1790 // wxTextUrlEvent passing if style & wxTE_AUTO_URL
1791 // ----------------------------------------------------------------------------
1793 // FIXME: when dragging on a link the sample gets an "Unknown event".
1794 // This might be an excessive event from us or a buggy wxMouseEvent::Moving() or
1795 // a buggy sample, or something else
1796 void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent
& event
)
1799 if( !HasFlag(wxTE_AUTO_URL
) )
1803 GtkTextIter start
, end
;
1804 GtkTextTag
*tag
= gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(m_buffer
),
1807 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(m_text
), GTK_TEXT_WINDOW_WIDGET
,
1808 event
.GetX(), event
.GetY(), &x
, &y
);
1810 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text
), &end
, x
, y
);
1811 if (!gtk_text_iter_has_tag(&end
, tag
))
1813 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1814 GTK_TEXT_WINDOW_TEXT
), m_gdkXTermCursor
);
1818 gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(m_text
),
1819 GTK_TEXT_WINDOW_TEXT
), m_gdkHandCursor
);
1822 if(!gtk_text_iter_begins_tag(&start
, tag
))
1823 gtk_text_iter_backward_to_tag_toggle(&start
, tag
);
1824 if(!gtk_text_iter_ends_tag(&end
, tag
))
1825 gtk_text_iter_forward_to_tag_toggle(&end
, tag
);
1827 // Native context menu is probably not desired on an URL.
1828 // Consider making this dependant on ProcessEvent(wxTextUrlEvent) return value
1829 if(event
.GetEventType() == wxEVT_RIGHT_DOWN
)
1832 wxTextUrlEvent
url_event(m_windowId
, event
,
1833 gtk_text_iter_get_offset(&start
),
1834 gtk_text_iter_get_offset(&end
));
1836 InitCommandEvent(url_event
);
1837 // Is that a good idea? Seems not (pleasure with gtk_text_view_start_selection_drag)
1838 //event.Skip(!GetEventHandler()->ProcessEvent(url_event));
1839 GetEventHandler()->ProcessEvent(url_event
);
1844 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1846 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);