]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/textctrl.cpp
use WXDLLIMPEXP_ADV on the base class too
[wxWidgets.git] / src / gtk / textctrl.cpp
index cd4b62035b27886efb1da884fd3ae2da9a13f1ba..8a2cd7bffe1eeaf6de5f5d0938684722a08df863 100644 (file)
@@ -16,7 +16,6 @@
     #include "wx/intl.h"
     #include "wx/log.h"
     #include "wx/utils.h"
-    #include "wx/panel.h"
     #include "wx/settings.h"
     #include "wx/math.h"
 #endif
@@ -29,7 +28,6 @@
 #include <ctype.h>
 
 #include "wx/gtk/private.h"
-#include <gdk/gdkkeysyms.h>
 
 // ----------------------------------------------------------------------------
 // helpers
@@ -54,7 +52,8 @@ static void wxGtkOnRemoveTag(GtkTextBuffer *buffer,
 }
 
 extern "C" {
-static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
+static void wxGtkTextApplyTagsFromAttr(GtkWidget *text,
+                                       GtkTextBuffer *text_buffer,
                                        const wxTextAttr& attr,
                                        GtkTextIter *start,
                                        GtkTextIter *end)
@@ -69,10 +68,9 @@ static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
 
     if (attr.HasFont())
     {
-        char *font_string;
         PangoFontDescription *font_description = attr.GetFont().GetNativeFontInfo()->description;
-        font_string = pango_font_description_to_string(font_description);
-        g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string);
+        wxGtkString font_string(pango_font_description_to_string(font_description));
+        g_snprintf(buf, sizeof(buf), "WXFONT %s", font_string.c_str());
         tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
                                          buf );
         if (!tag)
@@ -80,7 +78,6 @@ static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
                                               "font-desc", font_description,
                                               NULL );
         gtk_text_buffer_apply_tag (text_buffer, tag, start, end);
-        g_free (font_string);
 
         if (attr.GetFont().GetUnderlined())
         {
@@ -159,6 +156,109 @@ static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
                                               "justification", align, NULL );
         gtk_text_buffer_apply_tag( text_buffer, tag, &para_start, &para_end );
     }
+
+    if (attr.HasLeftIndent())
+    {
+        // Indentation attribute
+
+        // Clear old indentation tags
+        GtkTextIter para_start, para_end = *end;
+        gtk_text_buffer_get_iter_at_line( text_buffer,
+                                          &para_start,
+                                          gtk_text_iter_get_line(start) );
+        gtk_text_iter_forward_line(&para_end);
+
+        remove_handler_id = g_signal_connect (text_buffer, "remove_tag",
+                                              G_CALLBACK(wxGtkOnRemoveTag),
+                                              gpointer("WXINDENT"));
+        gtk_text_buffer_remove_all_tags( text_buffer, &para_start, &para_end );
+        g_signal_handler_disconnect (text_buffer, remove_handler_id);
+
+        // Convert indent from 1/10th of a mm into pixels
+        float factor;
+#if GTK_CHECK_VERSION(2,2,0)
+        if (!gtk_check_version(2,2,0))
+            factor = (float)gdk_screen_get_width(gtk_widget_get_screen(text)) /
+                      gdk_screen_get_width_mm(gtk_widget_get_screen(text)) / 10;
+        else
+#endif
+            factor = (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
+
+        const int indent = (int)(factor * attr.GetLeftIndent());
+        const int subIndent = (int)(factor * attr.GetLeftSubIndent());
+
+        gint gindent;
+        gint gsubindent;
+
+        if (subIndent >= 0)
+        {
+            gindent = indent;
+            gsubindent = -subIndent;
+        }
+        else
+        {
+            gindent = -subIndent;
+            gsubindent = indent;
+        }
+
+        g_snprintf(buf, sizeof(buf), "WXINDENT %d %d", gindent, gsubindent);
+        tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
+                                        buf );
+        if (!tag)
+            tag = gtk_text_buffer_create_tag( text_buffer, buf,
+                                              "left-margin", gindent, "indent", gsubindent, NULL );
+        gtk_text_buffer_apply_tag (text_buffer, tag, &para_start, &para_end);
+    }
+
+    if (attr.HasTabs())
+    {
+        // Set tab stops
+
+        // Clear old tabs
+        GtkTextIter para_start, para_end = *end;
+        gtk_text_buffer_get_iter_at_line( text_buffer,
+                                          &para_start,
+                                          gtk_text_iter_get_line(start) );
+        gtk_text_iter_forward_line(&para_end);
+
+        remove_handler_id = g_signal_connect (text_buffer, "remove_tag",
+                                              G_CALLBACK(wxGtkOnRemoveTag),
+                                              gpointer("WXTABS"));
+        gtk_text_buffer_remove_all_tags( text_buffer, &para_start, &para_end );
+        g_signal_handler_disconnect (text_buffer, remove_handler_id);
+
+        const wxArrayInt& tabs = attr.GetTabs();
+
+        wxString tagname = _T("WXTABS");
+        g_snprintf(buf, sizeof(buf), "WXTABS");
+        for (size_t i = 0; i < tabs.GetCount(); i++)
+            tagname += wxString::Format(_T(" %d"), tabs[i]);
+
+        const wxWX2MBbuf buf = tagname.mb_str(wxConvUTF8);
+
+        tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
+                                        buf );
+        if (!tag)
+        {
+            // Factor to convert from 1/10th of a mm into pixels
+            float factor;
+#if GTK_CHECK_VERSION(2,2,0)
+            if (!gtk_check_version(2,2,0))
+                factor = (float)gdk_screen_get_width(gtk_widget_get_screen(text)) /
+                          gdk_screen_get_width_mm(gtk_widget_get_screen(text)) / 10;
+            else
+#endif
+                factor = (float)gdk_screen_width() / gdk_screen_width_mm() / 10;
+
+            PangoTabArray* tabArray = pango_tab_array_new(tabs.GetCount(), TRUE);
+            for (size_t i = 0; i < tabs.GetCount(); i++)
+                pango_tab_array_set_tab(tabArray, i, PANGO_TAB_LEFT, (gint)(tabs[i] * factor));
+            tag = gtk_text_buffer_create_tag( text_buffer, buf,
+                                              "tabs", tabArray, NULL );
+            pango_tab_array_free(tabArray);
+        }
+        gtk_text_buffer_apply_tag (text_buffer, tag, &para_start, &para_end);
+    }
 }
 }
 
@@ -179,7 +279,7 @@ static void wxGtkTextInsert(GtkWidget *text,
 
     gtk_text_buffer_get_iter_at_offset (text_buffer, &start, start_offset);
 
-    wxGtkTextApplyTagsFromAttr(text_buffer, attr, &start, &iter);
+    wxGtkTextApplyTagsFromAttr(text, text_buffer, attr, &start, &iter);
 }
 }
 
@@ -450,9 +550,7 @@ gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
     if ( win->MarkDirtyOnChange() )
         win->MarkDirty();
 
-    wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
-    event.SetEventObject( win );
-    win->GetEventHandler()->ProcessEvent( event );
+    win->SendTextUpdatedEvent();
 }
 }
 
@@ -546,9 +644,10 @@ END_EVENT_TABLE()
 void wxTextCtrl::Init()
 {
     m_dontMarkDirty =
-    m_ignoreNextUpdate =
     m_modified = false;
 
+    m_countUpdatesToIgnore = 0;
+
     SetUpdateFont(false);
 
     m_text = NULL;
@@ -787,13 +886,11 @@ wxString wxTextCtrl::GetValue() const
         gtk_text_buffer_get_start_iter( m_buffer, &start );
         GtkTextIter end;
         gtk_text_buffer_get_end_iter( m_buffer, &end );
-        gchar *text = gtk_text_buffer_get_text( m_buffer, &start, &end, TRUE );
+        wxGtkString text(gtk_text_buffer_get_text(m_buffer, &start, &end, true));
 
         const wxWxCharBuffer buf = wxGTK_CONV_BACK(text);
         if ( buf )
             tmp = buf;
-
-        g_free( text );
     }
     else
     {
@@ -824,7 +921,15 @@ wxFontEncoding wxTextCtrl::GetTextEncoding() const
     return enc;
 }
 
-void wxTextCtrl::SetValue( const wxString &value )
+bool wxTextCtrl::IsEmpty() const
+{
+    if ( IsMultiLine() )
+        return gtk_text_buffer_get_char_count(m_buffer) != 0;
+
+    return wxTextCtrlBase::IsEmpty();
+}
+
+void wxTextCtrl::DoSetValue( const wxString &value, int flags )
 {
     wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
 
@@ -835,33 +940,39 @@ void wxTextCtrl::SetValue( const wxString &value )
     m_modified = false;
     DontMarkDirtyOnNextChange();
 
-    if ( IsMultiLine() )
+    const wxCharBuffer buffer(wxGTK_CONV_ENC(value, GetTextEncoding()));
+    if ( !buffer )
     {
-        const wxCharBuffer buffer(wxGTK_CONV_ENC(value, GetTextEncoding()));
-        if ( !buffer )
-        {
-            // see comment in WriteText() as to why we must warn the user about
-            // this
-            wxLogWarning(_("Failed to set text in the text control."));
-            return;
-        }
+        // see comment in WriteText() as to why we must warn the user about
+        // this
+        wxLogWarning(_("Failed to set text in the text control."));
+        return;
+    }
+
+    // if the control is not empty, two "changed" signals are emitted,
+    // otherwise only one and we need to ignore either both or one of them
+    int ignore = flags & SetValue_SendEvent ? 0 : 1;
+    if ( !IsEmpty() )
+        ignore++;
 
-        if (gtk_text_buffer_get_char_count(m_buffer) != 0)
-            IgnoreNextTextUpdate();
+    if ( ignore )
+        IgnoreNextTextUpdate(ignore);
 
+    if ( IsMultiLine() )
+    {
         gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
     }
     else // single line
     {
-        // gtk_entry_set_text() emits two "changed" signals if the control is
-        // not empty because internally it calls gtk_editable_delete_text() and
-        // gtk_editable_insert_text() but we want to have only one event
-        if ( !GetValue().empty() )
-            IgnoreNextTextUpdate();
-
-        gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV(value) );
+        gtk_entry_set_text( GTK_ENTRY(m_text), buffer );
     }
 
+    // if, for whatever reason, the callback wasn't called the expected number
+    // of times, still reset the flags to the default values
+    m_dontMarkDirty = false;
+    m_countUpdatesToIgnore = 0;
+
+
     // GRG, Jun/2000: Changed this after a lot of discussion in
     //   the lists. wxWidgets 2.2 will have a set of flags to
     //   customize this behaviour.
@@ -942,22 +1053,22 @@ void wxTextCtrl::AppendText( const wxString &text )
 
 wxString wxTextCtrl::GetLineText( long lineNo ) const
 {
+    wxString result;
     if ( IsMultiLine() )
     {
         GtkTextIter line;
         gtk_text_buffer_get_iter_at_line(m_buffer,&line,lineNo);
         GtkTextIter end = line;
         gtk_text_iter_forward_to_line_end(&end);
-        gchar *text = gtk_text_buffer_get_text(m_buffer,&line,&end,TRUE);
-        wxString result(wxGTK_CONV_BACK(text));
-        g_free(text);
-        return result;
+        wxGtkString text(gtk_text_buffer_get_text(m_buffer, &line, &end, true));
+        result = wxGTK_CONV_BACK(text);
     }
     else
     {
-        if (lineNo == 0) return GetValue();
-        return wxEmptyString;
+        if (lineNo == 0)
+            result = GetValue();
     }
+    return result;
 }
 
 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
@@ -1182,9 +1293,9 @@ void wxTextCtrl::DiscardEdits()
 
 bool wxTextCtrl::IgnoreTextUpdate()
 {
-    if ( m_ignoreNextUpdate )
+    if ( m_countUpdatesToIgnore > 0 )
     {
-        m_ignoreNextUpdate = false;
+        m_countUpdatesToIgnore--;
 
         return true;
     }
@@ -1648,7 +1759,7 @@ bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
         // colours for the others
         wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this);
 
-        wxGtkTextApplyTagsFromAttr( m_buffer, attr, &starti, &endi );
+        wxGtkTextApplyTagsFromAttr( m_widget, m_buffer, attr, &starti, &endi );
 
         return true;
     }
@@ -1739,7 +1850,10 @@ void wxTextCtrl::Freeze()
                               G_CALLBACK (gtk_text_exposed_callback), this);
             gtk_widget_set_sensitive(m_widget, false);
             g_object_ref(m_buffer);
-            gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), gtk_text_buffer_new(NULL));
+            GtkTextBuffer* buf_new = gtk_text_buffer_new(NULL);
+            gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_text), buf_new);
+            // gtk_text_view_set_buffer adds its own reference
+            g_object_unref(buf_new);
         }
     }
 }
@@ -1817,6 +1931,18 @@ void wxTextCtrl::OnUrlMouseEvent(wxMouseEvent& event)
     GetEventHandler()->ProcessEvent(url_event);
 }
 
+bool wxTextCtrl::GTKProcessEvent(wxEvent& event) const
+{
+    bool rc = wxTextCtrlBase::GTKProcessEvent(event);
+
+    // GtkTextView starts a drag operation when left mouse button is pressed
+    // and ends it when it is released and if it doesn't get the release event
+    // the next click on a control results in an assertion failure inside
+    // gtk_text_view_start_selection_drag() which simply *kills* the program
+    // without anything we can do about it, so always let GTK+ have this event
+    return rc && (IsSingleLine() || event.GetEventType() != wxEVT_LEFT_UP);
+}
+
 // static
 wxVisualAttributes
 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))