]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/textctrl.cpp
Bump version.
[wxWidgets.git] / src / gtk / textctrl.cpp
index 59ff4e0486f3efddd0dceec870f9e1b0f10894b0..cf14bf7797c23fe43825ff76cc3b1aafdde83425 100644 (file)
@@ -69,10 +69,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 +79,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())
         {
@@ -546,9 +544,10 @@ END_EVENT_TABLE()
 void wxTextCtrl::Init()
 {
     m_dontMarkDirty =
-    m_ignoreNextUpdate =
     m_modified = false;
 
+    m_countUpdatesToIgnore = 0;
+
     SetUpdateFont(false);
 
     m_text = NULL;
@@ -787,13 +786,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 +821,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") );
 
@@ -846,17 +851,40 @@ void wxTextCtrl::SetValue( const wxString &value )
             return;
         }
 
-        if (gtk_text_buffer_get_char_count(m_buffer) != 0)
-            IgnoreNextTextUpdate();
+
+        // if the control is not empty, two "changed" signals are emitted
+        if ( flags & SetValue_SendEvent )
+        {
+            if ( gtk_text_buffer_get_char_count(m_buffer) != 0 )
+                IgnoreNextTextUpdate();
+        }
+        else
+        {
+            if ( gtk_text_buffer_get_char_count(m_buffer) != 0 )
+                IgnoreNextTextUpdate(2);
+            else
+                IgnoreNextTextUpdate(1);        // skip only one
+        }
 
         gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
     }
     else // single line
     {
-        // gtk_entry_set_text() emits two "changed" signals because internally
-        // it calls gtk_editable_delete_text() and gtk_editable_insert_text()
-        // but we want to have only one event
-        IgnoreNextTextUpdate();
+        // 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()
+        if ( flags & SetValue_SendEvent )
+        {
+            if ( !GetValue().empty() )
+                IgnoreNextTextUpdate();
+        }
+        else
+        {
+            if ( !GetValue().empty() )
+                IgnoreNextTextUpdate(2);
+            else
+                IgnoreNextTextUpdate(1);        // if we are empty, skip only one event
+        }
 
         gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV(value) );
     }
@@ -941,22 +969,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) )
@@ -1181,9 +1209,9 @@ void wxTextCtrl::DiscardEdits()
 
 bool wxTextCtrl::IgnoreTextUpdate()
 {
-    if ( m_ignoreNextUpdate )
+    if ( m_countUpdatesToIgnore > 0 )
     {
-        m_ignoreNextUpdate = false;
+        m_countUpdatesToIgnore--;
 
         return true;
     }
@@ -1816,6 +1844,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))