]> spindle.queued.net Git - midori/commitdiff
Consider focus when toggling clear button
authorChristian Dywan <christian@twotoasts.de>
Sun, 19 Feb 2012 20:53:58 +0000 (21:53 +0100)
committerChristian Dywan <christian@twotoasts.de>
Sun, 19 Feb 2012 20:53:58 +0000 (21:53 +0100)
Fixes: https://bugs.launchpad.net/midori/+bug/934083
katze/gtk3-compat.c
katze/gtk3-compat.h
midori/sokoke.c

index 0c0a4369d67241fd706ca599c3a1d38cf0f44a07..8be809144af9f033ff18d6c069003a0cd0b28bc2 100644 (file)
@@ -27,9 +27,9 @@ sokoke_on_entry_focus_in_event (GtkEntry*      entry,
         g_object_get_data (G_OBJECT (entry), "sokoke_has_default"));
     if (has_default)
     {
-        gtk_entry_set_text (entry, "");
         g_object_set_data (G_OBJECT (entry), "sokoke_has_default",
                            GINT_TO_POINTER (0));
+        gtk_entry_set_text (entry, "");
         sokoke_widget_set_pango_font_style (GTK_WIDGET (entry),
                                             PANGO_STYLE_NORMAL);
     }
@@ -46,9 +46,9 @@ sokoke_on_entry_focus_out_event (GtkEntry*      entry,
     {
         const gchar* default_text = (const gchar*)g_object_get_data (
             G_OBJECT (entry), "sokoke_default_text");
-        gtk_entry_set_text (entry, default_text);
         g_object_set_data (G_OBJECT (entry),
                            "sokoke_has_default", GINT_TO_POINTER (1));
+        gtk_entry_set_text (entry, default_text);
         sokoke_widget_set_pango_font_style (GTK_WIDGET (entry),
                                             PANGO_STYLE_ITALIC);
     }
@@ -73,6 +73,9 @@ gtk_entry_set_placeholder_text (GtkEntry*    entry,
     /* Note: The default text initially overwrites any previous text */
     gchar* old_value = g_object_get_data (G_OBJECT (entry),
                                           "sokoke_default_text");
+    g_object_set_data (G_OBJECT (entry), "sokoke_default_text",
+                       (gpointer)default_text);
+
     if (!old_value)
     {
         g_object_set_data (G_OBJECT (entry), "sokoke_has_default",
@@ -98,8 +101,12 @@ gtk_entry_set_placeholder_text (GtkEntry*    entry,
                                                 PANGO_STYLE_ITALIC);
         }
     }
-    g_object_set_data (G_OBJECT (entry), "sokoke_default_text",
-                       (gpointer)default_text);
+}
+
+const gchar*
+gtk_entry_get_placeholder_text (GtkEntry* entry)
+{
+    return g_object_get_data (G_OBJECT (entry), "sokoke_default_text");
 }
 #endif
 
index fddfe977e8a30f73d776ab07f8c07864eedfeb79..70bc5c3f35056ef47065ed4dcfad0a4cf8922992 100644 (file)
@@ -89,8 +89,10 @@ G_BEGIN_DECLS
 
 #if !GTK_CHECK_VERSION (3, 2, 0) && defined (HAVE_HILDON_2_2)
     #define gtk_entry_set_placeholder_text hildon_gtk_entry_set_placeholder_text
+    #define gtk_entry_get_placeholder_text hildon_gtk_entry_get_placeholder_text
 #elif !GTK_CHECK_VERSION (3, 2, 0)
-    #define gtk_entry_set_placeholder_text sokoke_entry_set_default_text
+    void gtk_entry_set_placeholder_text (GtkEntry* entry, const gchar* text);
+    const gchar* gtk_entry_get_placeholder_text (GtkEntry* entry);
 #endif
 
 #if !GTK_CHECK_VERSION(2, 12, 0)
index e60087b57a4f5fe2ed4e241216915f88d900a861..3db12aaaa8284613d53fba246ad4f960c66163c6 100644 (file)
@@ -1731,18 +1731,40 @@ midori_download_prepare_tooltip_text (WebKitDownload* download)
     return g_string_free (tooltip, FALSE);
 }
 
+static gboolean
+sokoke_entry_has_placeholder_text (GtkEntry* entry)
+{
+    const gchar* text = gtk_entry_get_text (entry);
+    const gchar* hint = gtk_entry_get_placeholder_text (entry);
+    if (!gtk_widget_has_focus (GTK_WIDGET (entry))
+     && hint != NULL
+     && (text == NULL || !strcmp (text, hint)))
+        return TRUE;
+    return FALSE;
+}
+
 static void
 sokoke_entry_changed_cb (GtkEditable* editable,
                          GtkEntry*    entry)
 {
-    const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
-    gboolean visible = text && *text;
+    const gchar* text = gtk_entry_get_text (entry);
+    gboolean visible = text && *text
+      && ! sokoke_entry_has_placeholder_text (entry);
     gtk_icon_entry_set_icon_from_stock (
         GTK_ICON_ENTRY (entry),
         GTK_ICON_ENTRY_SECONDARY,
         visible ? GTK_STOCK_CLEAR : NULL);
 }
 
+static gboolean
+sokoke_entry_focus_out_event_cb (GtkEditable*   editable,
+                                 GdkEventFocus* event,
+                                 GtkEntry*      entry)
+{
+    sokoke_entry_changed_cb (editable, entry);
+    return FALSE;
+}
+
 static void
 sokoke_entry_icon_released_cb (GtkEntry*            entry,
                                GtkIconEntryPosition icon_pos,
@@ -1769,17 +1791,27 @@ sokoke_entry_set_clear_button_visible (GtkEntry* entry,
         g_object_connect (entry,
             "signal::icon-release",
             G_CALLBACK (sokoke_entry_icon_released_cb), NULL,
+            "signal::focus-in-event",
+            G_CALLBACK (sokoke_entry_focus_out_event_cb), entry,
+            "signal::focus-out-event",
+            G_CALLBACK (sokoke_entry_focus_out_event_cb), entry,
             "signal::changed",
             G_CALLBACK (sokoke_entry_changed_cb), entry, NULL);
-        g_signal_emit_by_name (G_OBJECT (entry), "changed");
+        sokoke_entry_changed_cb ((GtkEditable*)entry, entry);
     }
     else
     {
         g_object_disconnect (entry,
             "any_signal::icon-release",
             G_CALLBACK (sokoke_entry_icon_released_cb), NULL,
+            "any_signal::focus-in-event",
+            G_CALLBACK (sokoke_entry_focus_out_event_cb), entry,
+            "any_signal::focus-out-event",
+            G_CALLBACK (sokoke_entry_focus_out_event_cb), entry,
             "any_signal::changed",
             G_CALLBACK (sokoke_entry_changed_cb), entry, NULL);
+        gtk_icon_entry_set_icon_from_stock (
+            GTK_ICON_ENTRY (entry), GTK_ICON_ENTRY_SECONDARY, NULL);
     }
 }