[epiphany] Add suggestions to correct misspellings to the context menu



commit a0f0e5368807f17685ec8836be24eef43541e329
Author: Xan Lopez <xlopez igalia com>
Date:   Tue May 31 22:47:13 2011 +0200

    Add suggestions to correct misspellings to the context menu
    
    When the user right-clicks on a misspelled word a number of possible
    corrections will be offered by Epiphany. Selecting one of them will
    replace the misspelled word by the suggestion.

 data/ui/epiphany-ui.xml |    5 +++
 src/ephy-window.c       |   76 +++++++++++++++++++++++++++++++++++++++++++++--
 src/popup-commands.c    |   27 ++++++++++++++++
 src/popup-commands.h    |    4 ++
 4 files changed, 109 insertions(+), 3 deletions(-)
---
diff --git a/data/ui/epiphany-ui.xml b/data/ui/epiphany-ui.xml
index 90ae367..b9d415f 100644
--- a/data/ui/epiphany-ui.xml
+++ b/data/ui/epiphany-ui.xml
@@ -108,6 +108,11 @@
 	</menubar>
 
 	<popup name="EphyInputPopup" action="PopupAction">
+		<menuitem name="SpellingSuggestion0" action="ReplaceWithSpellingSuggestion0"/>
+		<menuitem name="SpellingSuggestion1" action="ReplaceWithSpellingSuggestion1"/>
+		<menuitem name="SpellingSuggestion2" action="ReplaceWithSpellingSuggestion2"/>
+		<menuitem name="SpellingSuggestion3" action="ReplaceWithSpellingSuggestion3"/>
+		<separator name="SpellingSeparator"/>
 		<menuitem name="EditCutIP" action="EditCut"/>
 		<menuitem name="EditCopyIP" action="EditCopy"/>
 		<menuitem name="EditPasteIP" action="EditPaste"/>
diff --git a/src/ephy-window.c b/src/ephy-window.c
index a4c8bec..38f1613 100644
--- a/src/ephy-window.c
+++ b/src/ephy-window.c
@@ -357,6 +357,18 @@ static const GtkActionEntry ephy_popups_entries [] = {
 	{ "StopImageAnimation", NULL, N_("St_op Animation"), NULL,
 	  NULL, NULL },
 
+	/* Spelling */
+
+	{ "ReplaceWithSpellingSuggestion0", NULL, NULL, NULL,
+	  NULL, G_CALLBACK (popup_replace_spelling), },
+	{ "ReplaceWithSpellingSuggestion1", NULL, NULL, NULL,
+	  NULL, G_CALLBACK (popup_replace_spelling), },
+	{ "ReplaceWithSpellingSuggestion2", NULL, NULL, NULL,
+	  NULL, G_CALLBACK (popup_replace_spelling), },
+	{ "ReplaceWithSpellingSuggestion3", NULL, NULL, NULL,
+	  NULL, G_CALLBACK (popup_replace_spelling), },
+
+
 	/* Inspector */
 	{ "InspectElement", NULL, N_("Inspect _Element"), NULL,
 	  NULL, G_CALLBACK (popup_cmd_inspect_element) },
@@ -1124,13 +1136,21 @@ ephy_window_delete_event (GtkWidget *widget,
 	return FALSE;
 }
 
+#define MAX_SPELL_CHECK_GUESSES 4
+
 static void
 update_popup_actions_visibility (EphyWindow *window,
-				 gboolean is_image,
+				 WebKitWebView *view,
+				 guint context,
 				 gboolean is_frame)
 {
 	GtkAction *action;
 	GtkActionGroup *action_group;
+	gboolean is_image = context & WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE;
+	gboolean is_editable = context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE;
+	GtkWidget *separator;
+	char **guesses = NULL;
+	int i;
 
 	action_group = window->priv->popups_action_group;
 
@@ -1145,6 +1165,56 @@ update_popup_actions_visibility (EphyWindow *window,
 
 	action = gtk_action_group_get_action (action_group, "OpenFrame");
 	gtk_action_set_visible (action, is_frame);
+
+	if (is_editable)
+	{
+		char *text = NULL;
+		WebKitWebFrame *frame;
+		WebKitDOMRange *range;
+
+		frame = webkit_web_view_get_focused_frame (view);
+		range = webkit_web_frame_get_range_for_word_around_caret (frame);
+		text = webkit_dom_range_get_text (range);
+
+		if (text)
+		{
+			int location, length;
+			WebKitSpellChecker *checker = webkit_get_text_checker();
+			webkit_spell_checker_check_spelling_of_string (checker, text, &location, &length);
+			if (length)
+				guesses = webkit_spell_checker_get_guesses_for_word (checker, text, NULL);
+			
+		}
+
+		g_free (text);
+	}
+
+	for (i = 0; i < MAX_SPELL_CHECK_GUESSES; i++)
+	{
+		char *action_name;
+
+		action_name = g_strdup_printf("ReplaceWithSpellingSuggestion%d", i);
+		action = gtk_action_group_get_action (action_group, action_name);
+
+		if (guesses && i <= g_strv_length (guesses)) {
+			gtk_action_set_visible (action, TRUE);
+			gtk_action_set_label (action, guesses[i]);
+		} else
+			gtk_action_set_visible (action, FALSE);
+
+		g_free (action_name);
+	}
+
+	/* The separator! There must be a better way to do this? */
+	separator = gtk_ui_manager_get_widget (window->priv->manager,
+					       "/EphyInputPopup/SpellingSeparator");
+	if (guesses)
+		gtk_widget_show (separator);
+	else
+		gtk_widget_hide (separator);
+
+	if (guesses)
+		g_strfreev (guesses);
 }
 
 static void
@@ -2199,10 +2269,10 @@ show_embed_popup (EphyWindow *window,
 
 	action = gtk_action_group_get_action (action_group, "OpenLinkInNewTab");
 	ephy_action_change_sensitivity_flags (action, SENS_FLAG_CONTEXT, !can_open_in_new);
-
 	
 	update_popup_actions_visibility (window,
-					 context & WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
+					 view,
+					 context,
 					 framed);
 
 	embed_event = ephy_embed_event_new (event, hit_test_result);
diff --git a/src/popup-commands.c b/src/popup-commands.c
index e7064f1..14867c4 100644
--- a/src/popup-commands.c
+++ b/src/popup-commands.c
@@ -479,6 +479,33 @@ save_temp_source (const char *address)
 }
 
 void
+popup_replace_spelling (GtkAction *action,
+			EphyWindow *window)
+{
+	EphyEmbed *embed;
+	WebKitWebView *view;
+	WebKitWebFrame *frame;
+	WebKitDOMDOMSelection *selection;
+	WebKitDOMDocument *document;
+	WebKitDOMDOMWindow *default_view;
+
+	embed = ephy_embed_container_get_active_child 
+		(EPHY_EMBED_CONTAINER (window));
+	g_return_if_fail (embed != NULL);
+
+	view = WEBKIT_WEB_VIEW (ephy_embed_get_web_view (embed));
+	g_return_if_fail (view != NULL);
+
+	document = webkit_web_view_get_dom_document (view);
+	default_view = webkit_dom_document_get_default_view (document);
+	selection = webkit_dom_dom_window_get_selection (default_view);
+	webkit_dom_dom_selection_modify (selection, "move", "backward", "word");
+	webkit_dom_dom_selection_modify (selection, "extend", "forward", "word");
+	frame = webkit_web_view_get_focused_frame (view);
+	webkit_web_frame_replace_selection (frame, gtk_action_get_label (action));
+}
+
+void
 popup_cmd_open_image (GtkAction *action,
 		      EphyWindow *window)
 {
diff --git a/src/popup-commands.h b/src/popup-commands.h
index 02d44c1..f180564 100644
--- a/src/popup-commands.h
+++ b/src/popup-commands.h
@@ -68,3 +68,7 @@ void popup_cmd_save_image_as		(GtkAction *action,
 
 void popup_cmd_inspect_element		(GtkAction *action,
 					 EphyWindow *window);
+
+void popup_replace_spelling             (GtkAction *action,
+                                         EphyWindow *window);
+



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