[latexila] ErrorEntry: set error for a text entry



commit cea3fdb5272d2d73c2c1f8dfd9fd16ae80adb12b
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Fri Apr 6 08:19:23 2012 +0200

    ErrorEntry: set error for a text entry
    
    Visually, the background becomes red when the error is set.
    It is used e.g. for the find entry in the search and replace when the
    text is not found.

 src/build_tool_dialog.vala |   32 ++++++-----------------
 src/error_entry.vala       |   59 ++++++++++++++++++++++++++++++++++++++++++++
 src/search.vala            |   17 ++++++------
 src/utils.vala             |   10 -------
 4 files changed, 76 insertions(+), 42 deletions(-)
---
diff --git a/src/build_tool_dialog.vala b/src/build_tool_dialog.vala
index 7842cfa..3d61bea 100644
--- a/src/build_tool_dialog.vala
+++ b/src/build_tool_dialog.vala
@@ -24,11 +24,11 @@ private class BuildToolDialog : Dialog
 {
     private static BuildToolDialog _instance = null;
 
-    private Entry _entry_label;
+    private ErrorEntry _entry_label;
     private Entry _entry_desc;
     private Entry _entry_extensions;
     private ComboBox _combobox_icon;
-    private Entry _entry_command;
+    private ErrorEntry _entry_command;
     private Button _button_add;
     private TreeView _treeview_jobs;
     private Button _button_delete;
@@ -135,7 +135,7 @@ private class BuildToolDialog : Dialog
         grid.attach (arrow, 1, 0, 1, 1);
         grid.set_hexpand (false);
 
-        _entry_label = new Entry ();
+        _entry_label = new ErrorEntry ();
         _entry_label.set_margin_left (12);
         grid.attach (_entry_label, 0, 1, 2, 1);
 
@@ -179,7 +179,7 @@ private class BuildToolDialog : Dialog
         placeholder_view.set_tooltip_text (
             _("The program for viewing documents.\nIts value can be changed in the preferences dialog."));
 
-        _entry_command = new Entry ();
+        _entry_command = new ErrorEntry ();
         _entry_command.hexpand = true;
 
         _button_add = new Button.from_stock (Stock.ADD);
@@ -384,8 +384,8 @@ private class BuildToolDialog : Dialog
     {
         _entry_command.text = "";
         _jobs_store.clear ();
-        Utils.set_entry_error (_entry_label, false);
-        Utils.set_entry_error (_entry_command, false);
+        _entry_label.error = false;
+        _entry_command.error = false;
 
         if (build_tool_num == -1)
             _instance.init_new_build_tool ();
@@ -442,28 +442,14 @@ private class BuildToolDialog : Dialog
         {
             /* check if the form is correctly filled */
 
-            bool ok = true;
-
             // no label
-            if (_entry_label.text.strip () == "")
-            {
-                Utils.set_entry_error (_entry_label, true);
-                ok = false;
-            }
-            else
-                Utils.set_entry_error (_entry_label, false);
+            _entry_label.error = _entry_label.text.strip () == "";
 
             // no job
             TreeIter iter;
-            if (! _jobs_store.get_iter_first (out iter))
-            {
-                Utils.set_entry_error (_entry_command, true);
-                ok = false;
-            }
-            else
-                Utils.set_entry_error (_entry_command, false);
+            _entry_command.error = ! _jobs_store.get_iter_first (out iter);
 
-            if (! ok)
+            if (_entry_label.error || _entry_command.error)
                 continue;
 
             /* generate a new build tool */
diff --git a/src/error_entry.vala b/src/error_entry.vala
new file mode 100644
index 0000000..11a0c47
--- /dev/null
+++ b/src/error_entry.vala
@@ -0,0 +1,59 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright  2012 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Gtk;
+
+// A simple text entry for which we can set visually that there is an error.
+public class ErrorEntry : Entry
+{
+    public bool error { get; set; default = false; }
+
+    public ErrorEntry ()
+    {
+        string style = """
+        GtkEntry {
+            color: @error_fg_color;
+            background-image: none;
+            background-color: @error_bg_color;
+        }
+        """;
+
+        CssProvider provider = new CssProvider ();
+
+        try
+        {
+            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 ();
+
+            if (error)
+                context.add_provider (provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
+            else
+                context.remove_provider (provider);
+        });
+    }
+}
diff --git a/src/search.vala b/src/search.vala
index 86e025d..2931f9b 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 Entry entry;
+    private ErrorEntry entry;
 
     public GotoLine (MainWindow main_window)
     {
@@ -42,7 +42,7 @@ public class GotoLine : Grid
         label.margin_right = 2;
         add (label);
 
-        entry = new Entry ();
+        entry = new ErrorEntry ();
         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)
         {
-            Utils.set_entry_error (entry, false);
+            entry.error = false;
             return;
         }
 
@@ -77,14 +77,13 @@ public class GotoLine : Grid
             unichar c = text[i];
             if (! c.isdigit ())
             {
-                Utils.set_entry_error (entry, true);
+                entry.error = true;
                 return;
             }
         }
 
         int line = int.parse (text);
-        bool error = ! main_window.active_document.goto_line (--line);
-        Utils.set_entry_error (entry, error);
+        entry.error = ! main_window.active_document.goto_line (--line);
         main_window.active_view.scroll_to_cursor ();
     }
 }
@@ -100,7 +99,7 @@ public class SearchAndReplace : GLib.Object
     private Button _button_arrow;
     private Arrow _arrow;
 
-    private Entry _entry_find;
+    private ErrorEntry _entry_find;
     private Entry _entry_replace;
 
     private CheckMenuItem _check_case_sensitive;
@@ -302,7 +301,7 @@ public class SearchAndReplace : GLib.Object
     /* Find entry */
     private void init_find_entry ()
     {
-        _entry_find = new Entry ();
+        _entry_find = new ErrorEntry ();
         _entry_find.primary_icon_gicon =
             new ThemedIcon.with_default_fallbacks ("document-properties-symbolic");
         _entry_find.primary_icon_activatable = true;
@@ -405,7 +404,7 @@ public class SearchAndReplace : GLib.Object
         _working_document.set_search_text (_entry_find.text, case_sensitive,
             entire_word, out nb_matches, null, select);
 
-        Utils.set_entry_error (_entry_find, nb_matches == 0);
+        _entry_find.error = nb_matches == 0;
     }
 
     private void select_current_match ()
diff --git a/src/utils.vala b/src/utils.vala
index c984f0f..24fab78 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -304,16 +304,6 @@ namespace Utils
         return scrollbar;
     }
 
-    public void set_entry_error (Widget entry, bool error)
-    {
-        StyleContext context = entry.get_style_context ();
-
-        if (error)
-            context.add_class ("not-found");
-        else
-            context.remove_class ("not-found");
-    }
-
     public bool tree_model_iter_prev (TreeModel model, ref TreeIter iter)
     {
         TreePath path = model.get_path (iter);



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