[latexila] ErrorEntry: static functions instead of inheritance



commit 33a2332520e6cf617ee9460e5485ca9e83e73a5f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Oct 4 16:37:34 2013 +0200

    ErrorEntry: static functions instead of inheritance
    
    So the ErrorEntry can be used for a GtkSearchEntry or a GdTaggedEntry
    too.

 src/error_entry.vala |   45 +++++++++++++++++++++++++++++----------------
 src/search.vala      |   17 +++++++++++------
 2 files changed, 40 insertions(+), 22 deletions(-)
---
diff --git a/src/error_entry.vala b/src/error_entry.vala
index 95950ca..c89c178 100644
--- a/src/error_entry.vala
+++ b/src/error_entry.vala
@@ -1,7 +1,7 @@
 /*
  * This file is part of LaTeXila.
  *
- * Copyright © 2012 Sébastien Wilmet
+ * Copyright © 2012, 2013 Sébastien Wilmet
  *
  * LaTeXila is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,13 +19,17 @@
 
 using Gtk;
 
-// A simple text entry for which we can set visually that there is an error.
-public class ErrorEntry : Entry
+// Add and remove a style for errors (e.g. text not found) in a GtkEntry.
+// The style is: red background, white foreground.
+public class ErrorEntry : Object
 {
-    public bool error { get; set; default = false; }
+    private static CssProvider _provider = null;
 
-    public ErrorEntry ()
+    private static void init_provider ()
     {
+        if (_provider != null)
+            return;
+
         string style = """
         GtkEntry {
             color: white;
@@ -34,26 +38,35 @@ public class ErrorEntry : Entry
         }
         """;
 
-        CssProvider provider = new CssProvider ();
+        _provider = new CssProvider ();
 
         try
         {
-            provider.load_from_data (style, -1);
+            _provider.load_from_data (style, -1);
         }
         catch (Error e)
         {
             warning ("Impossible to load CSS style for the error entry: %s", e.message);
-            return;
         }
+    }
 
-        notify["error"].connect (() =>
-        {
-            StyleContext context = get_style_context ();
+    public static void add_error (Widget widget)
+    {
+        StyleContext context = widget.get_style_context ();
+
+        init_provider ();
+
+        if (_provider != null)
+            context.add_provider (_provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
+
+    public static void remove_error (Widget widget)
+    {
+        StyleContext context = widget.get_style_context ();
+
+        init_provider ();
 
-            if (error)
-                context.add_provider (provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
-            else
-                context.remove_provider (provider);
-        });
+        if (_provider != null)
+            context.remove_provider (_provider);
     }
 }
diff --git a/src/search.vala b/src/search.vala
index e2f35bd..6741f5e 100644
--- a/src/search.vala
+++ b/src/search.vala
@@ -22,7 +22,7 @@ using Gtk;
 public class GotoLine : Grid
 {
     private unowned MainWindow main_window;
-    private ErrorEntry entry;
+    private Entry entry;
 
     public GotoLine (MainWindow main_window)
     {
@@ -42,7 +42,7 @@ public class GotoLine : Grid
         label.margin_right = 2;
         add (label);
 
-        entry = new ErrorEntry ();
+        entry = new Entry ();
         add (entry);
         Icon icon = new ThemedIcon.with_default_fallbacks ("go-jump-symbolic");
         entry.set_icon_from_gicon (EntryIconPosition.SECONDARY, icon);
@@ -65,7 +65,7 @@ public class GotoLine : Grid
     {
         if (entry.text_length == 0)
         {
-            entry.error = false;
+            ErrorEntry.remove_error (entry);
             return;
         }
 
@@ -77,13 +77,18 @@ public class GotoLine : Grid
             unichar c = text[i];
             if (! c.isdigit ())
             {
-                entry.error = true;
+                ErrorEntry.add_error (entry);
                 return;
             }
         }
 
-        int line = int.parse (text);
-        entry.error = ! main_window.active_document.goto_line (--line);
+        int line = int.parse (text) - 1;
+
+        if (main_window.active_document.goto_line (line))
+            ErrorEntry.remove_error (entry);
+        else
+            ErrorEntry.add_error (entry);
+
         main_window.active_view.scroll_to_cursor ();
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]