From e4473fa1939e8f41b92de39e4162be16ff72aedb Mon Sep 17 00:00:00 2001 From: Andreas Falkenhahn Date: Sun, 20 Mar 2016 15:48:52 +0100 Subject: [PATCH] Apply wxTextCtrl::SetDefaultStyle() to user-entered text in wxGTK Apply the appropriate style to any text being inserted, not just the text added by the program. This not only fixes the bug with default style not being used for the text entered by user, but also makes the code simpler. Closes #17523. --- docs/changes.txt | 1 + src/gtk/textctrl.cpp | 53 +++++++++++++++----------------------------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index ea48051412..c29def0fd8 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -91,6 +91,7 @@ All (GUI): wxGTK: - Make wxUIActionSimulator work with GTK+ 3 (Scott Talbert). +- Apply wxTextCtrl::SetDefaultStyle() to user-entered text (Andreas Falkenhahn). - Implement setting link colours in wxHyperlinkCtrl for GTK+3 (Hanmac). - Support background colour in wxDataViewCtrl attributes. - Improve wxSpinCtrl best size calculation. diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index 03effd8c1f..07082f9b48 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -288,25 +288,6 @@ static void wxGtkTextApplyTagsFromAttr(GtkWidget *text, } } -static void wxGtkTextInsert(GtkWidget *text, - GtkTextBuffer *text_buffer, - const wxTextAttr& attr, - const wxCharBuffer& buffer) - -{ - gint start_offset; - GtkTextIter iter, start; - - gtk_text_buffer_get_iter_at_mark( text_buffer, &iter, - gtk_text_buffer_get_insert (text_buffer) ); - start_offset = gtk_text_iter_get_offset (&iter); - gtk_text_buffer_insert( text_buffer, &iter, buffer, strlen(buffer) ); - - gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset); - - wxGtkTextApplyTagsFromAttr(text, text_buffer, attr, &start, &iter); -} - // Implementation of wxTE_AUTO_URL for wxGTK2 by Mart Raudsepp, extern "C" { @@ -480,18 +461,24 @@ wx_insert_text_callback(GtkTextBuffer* buffer, // And an "after" version used for detecting URLs in the text. static void -au_insert_text_callback(GtkTextBuffer * WXUNUSED(buffer), +au_insert_text_callback(GtkTextBuffer *buffer, GtkTextIter *end, gchar *text, gint len, wxTextCtrl *win) { - if (!len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) ) - return; - GtkTextIter start = *end; gtk_text_iter_backward_chars(&start, g_utf8_strlen(text, len)); + if ( !win->GetDefaultStyle().IsDefault() ) + { + wxGtkTextApplyTagsFromAttr(win->GetHandle(), buffer, win->GetDefaultStyle(), + &start, end); + } + + if ( !len || !(win->GetWindowStyleFlag() & wxTE_AUTO_URL) ) + return; + GtkTextIter line_start = start; GtkTextIter line_end = *end; GtkTextIter words_start = start; @@ -799,9 +786,6 @@ bool wxTextCtrl::Create( wxWindow *parent, "underline", PANGO_UNDERLINE_SINGLE, NULL); - // Check for URLs after each text change - g_signal_connect_after (m_buffer, "insert_text", - G_CALLBACK (au_insert_text_callback), this); g_signal_connect_after (m_buffer, "delete_range", G_CALLBACK (au_delete_range_callback), this); @@ -824,6 +808,10 @@ bool wxTextCtrl::Create( wxWindow *parent, // the IME-generated input. g_signal_connect(m_buffer, "insert_text", G_CALLBACK(wx_insert_text_callback), this); + + // Needed for wxTE_AUTO_URL and applying custom styles + g_signal_connect_after(m_buffer, "insert_text", + G_CALLBACK(au_insert_text_callback), this); } else // single line { @@ -1088,14 +1076,6 @@ void wxTextCtrl::DoSetValue( const wxString &value, int flags ) gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) ); - if ( !m_defaultStyle.IsDefault() ) - { - GtkTextIter start, end; - gtk_text_buffer_get_bounds( m_buffer, &start, &end ); - wxGtkTextApplyTagsFromAttr(m_widget, m_buffer, m_defaultStyle, - &start, &end); - } - if ( !(flags & SetValue_SendEvent) ) { EnableTextChangedEvents(true); @@ -1149,7 +1129,10 @@ void wxTextCtrl::WriteText( const wxString &text ) gtk_text_buffer_delete_selection(m_buffer, false, true); // Insert the text - wxGtkTextInsert( m_text, m_buffer, m_defaultStyle, buffer ); + GtkTextIter iter; + gtk_text_buffer_get_iter_at_mark( m_buffer, &iter, + gtk_text_buffer_get_insert (m_buffer) ); + gtk_text_buffer_insert( m_buffer, &iter, buffer, strlen(buffer) ); // Scroll to cursor, but only if scrollbar thumb is at the very bottom // won't work when frozen, text view is not using m_buffer then