[latexila] Spell checking with GtkSpell



commit 86b857e43db4eba39e0b6d26efaddcbf50901a31
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Sat Sep 17 04:52:17 2011 +0200

    Spell checking with GtkSpell
    
    Thanks Dion Timmermann!

 CMakeLists.txt                      |    3 ++
 INSTALL                             |    1 +
 data/org.gnome.latexila.gschema.xml |    4 ++
 src/document_view.vala              |   64 ++++++++++++++++++++++++----------
 src/main_window.vala                |   25 +++++++++++++
 src/ui/ui.xml                       |    2 +
 vapi/gtkspell-2.0.vapi              |   16 +++++++++
 7 files changed, 96 insertions(+), 19 deletions(-)
---
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 394a59d..1457ee2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -43,6 +43,7 @@ endif ()
 find_package (PkgConfig)
 pkg_check_modules (GTK REQUIRED "gtk+-2.0 >= 2.16")
 pkg_check_modules (GTKSOURCEVIEW REQUIRED "gtksourceview-2.0 >= 2.10")
+pkg_check_modules (GTKSPELL REQUIRED "gtkspell-2.0")
 pkg_check_modules (GEE REQUIRED "gee-1.0")
 pkg_check_modules (GIO REQUIRED "gio-2.0 >= 2.28")
 pkg_check_modules (UNIQUE REQUIRED "unique-1.0")
@@ -63,6 +64,7 @@ add_definitions (-include config.h)
 add_definitions (
 	${GTK_CFLAGS}
 	${GTKSOURCEVIEW_CFLAGS}
+	${GTKSPELL_CFLAGS}
 	${GEE_CFLAGS}
 	${GIO_CFLAGS}
 	${UNIQUE_CFLAGS}
@@ -73,6 +75,7 @@ add_definitions (
 link_libraries (
 	${GTK_LIBRARIES}
 	${GTKSOURCEVIEW_LIBRARIES}
+	${GTKSPELL_LIBRARIES}
 	${GEE_LIBRARIES}
 	${GIO_LIBRARIES}
 	${UNIQUE_LIBRARIES}
diff --git a/INSTALL b/INSTALL
index 447eb63..c94498e 100644
--- a/INSTALL
+++ b/INSTALL
@@ -6,6 +6,7 @@ GtkSourceView >= 2.10
 GLib >= 2.28
 libgee
 unique
+GtkSpell
 gdk-x11
 x11
 CMake >= 2.6.4
diff --git a/data/org.gnome.latexila.gschema.xml b/data/org.gnome.latexila.gschema.xml
index 43e539a..e961ecf 100644
--- a/data/org.gnome.latexila.gschema.xml
+++ b/data/org.gnome.latexila.gschema.xml
@@ -84,6 +84,10 @@
       <default>15</default>
       <summary>Number of most used symbols</summary>
     </key>
+    <key name="spell-checking" type="b">
+      <default>false</default>
+      <summary>Spell checking</summary>
+    </key>
   </schema>
 
   <schema id="org.gnome.latexila.preferences.ui" path="/org/gnome/latexila/preferences/ui/">
diff --git a/src/document_view.vala b/src/document_view.vala
index 881e8fd..6946bc5 100644
--- a/src/document_view.vala
+++ b/src/document_view.vala
@@ -23,8 +23,8 @@ public class DocumentView : Gtk.SourceView
 {
     public const double SCROLL_MARGIN = 0.02;
 
-    private GLib.Settings editor_settings;
-    private Pango.FontDescription font_desc;
+    private GLib.Settings _editor_settings;
+    private Pango.FontDescription _font_desc;
 
     public DocumentView (Document doc)
     {
@@ -40,21 +40,21 @@ public class DocumentView : Gtk.SourceView
         indent_width = -1;
 
         /* settings */
-        editor_settings = new GLib.Settings ("org.gnome.latexila.preferences.editor");
+        _editor_settings = new GLib.Settings ("org.gnome.latexila.preferences.editor");
 
         set_font_from_settings ();
 
         // tab width
         uint tmp;
-        editor_settings.get ("tabs-size", "u", out tmp);
+        _editor_settings.get ("tabs-size", "u", out tmp);
         tab_width = tmp;
 
-        insert_spaces_instead_of_tabs = editor_settings.get_boolean ("insert-spaces");
-        show_line_numbers = editor_settings.get_boolean ("display-line-numbers");
-        highlight_current_line = editor_settings.get_boolean ("highlight-current-line");
+        insert_spaces_instead_of_tabs = _editor_settings.get_boolean ("insert-spaces");
+        show_line_numbers = _editor_settings.get_boolean ("display-line-numbers");
+        highlight_current_line = _editor_settings.get_boolean ("highlight-current-line");
         doc.highlight_matching_brackets =
-            editor_settings.get_boolean ("bracket-matching");
-        doc.set_style_scheme_from_string (editor_settings.get_string ("scheme"));
+            _editor_settings.get_boolean ("bracket-matching");
+        doc.set_style_scheme_from_string (_editor_settings.get_string ("scheme"));
         set_smart_home_end (SourceSmartHomeEndType.AFTER);
 
         // completion
@@ -79,6 +79,10 @@ public class DocumentView : Gtk.SourceView
 
         // smart backspace (if indent with spaces)
         key_press_event.connect (on_backspace);
+
+        // spell checking
+        if (_editor_settings.get_boolean ("spell-checking"))
+            activate_spell_checking ();
     }
 
     public void scroll_to_cursor (double margin = 0.25)
@@ -154,32 +158,32 @@ public class DocumentView : Gtk.SourceView
     public void set_font_from_settings ()
     {
         string font;
-        if (editor_settings.get_boolean ("use-default-font"))
+        if (_editor_settings.get_boolean ("use-default-font"))
             font = AppSettings.get_default ().system_font;
         else
-            font = editor_settings.get_string ("editor-font");
+            font = _editor_settings.get_string ("editor-font");
 
         set_font_from_string (font);
     }
 
     public void set_font_from_string (string font)
     {
-        font_desc = Pango.FontDescription.from_string (font);
-        modify_font (font_desc);
+        _font_desc = Pango.FontDescription.from_string (font);
+        modify_font (_font_desc);
     }
 
     public void enlarge_font ()
     {
         // this is not saved in the settings
-        font_desc.set_size (font_desc.get_size () + Pango.SCALE);
-        modify_font (font_desc);
+        _font_desc.set_size (_font_desc.get_size () + Pango.SCALE);
+        modify_font (_font_desc);
     }
 
     public void shrink_font ()
     {
         // this is not saved in the settings
-        font_desc.set_size (font_desc.get_size () - Pango.SCALE);
-        modify_font (font_desc);
+        _font_desc.set_size (_font_desc.get_size () - Pango.SCALE);
+        modify_font (_font_desc);
     }
 
     public string get_indentation_style ()
@@ -189,14 +193,36 @@ public class DocumentView : Gtk.SourceView
         return "\t";
     }
 
+    public void activate_spell_checking ()
+    {
+        disable_spell_checking ();
+
+        try
+        {
+            // Will try the best language depending on the LANG environment variable.
+            new GtkSpell.attach (this, null);
+        }
+        catch (GtkspellError e)
+        {
+            warning ("Spell error: %s", e.message);
+        }
+    }
+
+    public void disable_spell_checking ()
+    {
+        GtkSpell? spell = GtkSpell.get_from_text_view (this);
+        if (spell != null)
+            spell.detach ();
+    }
+
     private bool on_backspace (Gdk.EventKey event)
     {
         // See GDK_KEY_BackSpace in gdk/gdkkeysyms.h (not available in Vala)
 
         // TODO~ connect/disconnect the signal when settings in gsettings change
         // note: this function will be removed when latexila will become a Gedit plugin...
-        if (! editor_settings.get_boolean ("insert-spaces")
-            || ! editor_settings.get_boolean ("forget-no-tabs")
+        if (! _editor_settings.get_boolean ("insert-spaces")
+            || ! _editor_settings.get_boolean ("forget-no-tabs")
             || event.keyval != 0xff08
             || buffer.has_selection
             || tab_width == 1)
diff --git a/src/main_window.vala b/src/main_window.vala
index 868b839..4900d9d 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -165,6 +165,8 @@ public class MainWindow : Window
 
     private const ToggleActionEntry[] toggle_action_entries =
     {
+        { "EditSpellChecking", Stock.SPELL_CHECK, null, "",
+            N_("Activate or disable the spell checking"), on_spell_checking },
         { "ViewMainToolbar", null, N_("_Main Toolbar"), null,
             N_("Show or hide the main toolbar"), on_show_main_toolbar },
         // Translators: "Edit" here is an adjective.
@@ -427,6 +429,16 @@ public class MainWindow : Window
         set_file_actions_sensitivity (false);
         set_documents_move_to_new_window_sensitivity (false);
 
+        // spell checking
+        ToggleAction spell_checking_action =
+            action_group.get_action ("EditSpellChecking") as ToggleAction;
+
+        GLib.Settings editor_settings =
+            new GLib.Settings ("org.gnome.latexila.preferences.editor");
+
+        editor_settings.bind ("spell-checking", spell_checking_action, "active",
+            SettingsBindFlags.DEFAULT);
+
         /* packing widgets */
         VBox main_vbox = new VBox (false, 0);
         main_vbox.pack_start (menu, false, false, 0);
@@ -1636,6 +1648,19 @@ public class MainWindow : Window
         active_view.show_completion ();
     }
 
+    public void on_spell_checking (Gtk.Action action)
+    {
+        bool activate = (action as ToggleAction).active;
+
+        foreach (DocumentView view in get_views ())
+        {
+            if (activate)
+                view.activate_spell_checking ();
+            else
+                view.disable_spell_checking ();
+        }
+    }
+
     public void on_open_preferences ()
     {
         PreferencesDialog.show_me (this);
diff --git a/src/ui/ui.xml b/src/ui/ui.xml
index 484c95a..a2cbef7 100644
--- a/src/ui/ui.xml
+++ b/src/ui/ui.xml
@@ -51,6 +51,8 @@ along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
       <separator />
       <menuitem action="EditCompletion" />
       <separator />
+      <menuitem action="EditSpellChecking" />
+      <separator />
       <menuitem action="EditPreferences" />
     </menu>
 
diff --git a/vapi/gtkspell-2.0.vapi b/vapi/gtkspell-2.0.vapi
new file mode 100644
index 0000000..1658454
--- /dev/null
+++ b/vapi/gtkspell-2.0.vapi
@@ -0,0 +1,16 @@
+[CCode (cheader_filename = "gtkspell/gtkspell.h")]
+public errordomain GtkspellError
+{
+    BACKEND
+}
+
+[Compact]
+[CCode (cprefix = "gtkspell_", cheader_filename = "gtkspell/gtkspell.h", free_function = "")]
+public class GtkSpell
+{
+    public GtkSpell.attach (Gtk.TextView view, string? lang) throws GtkspellError;
+    public static GtkSpell? get_from_text_view (Gtk.TextView view);
+    public void detach ();
+    public bool set_language (string lang) throws GtkspellError;
+    public void recheck_all ();
+}



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