TX Spell .NET for WPF

Synonyms in the Context Menu

Topic

With TX Spell .NET connected, TX Text Control offers synonyms through its thesaurus: the user selects a word, clicks Thesaurus on the ribbon and picks a replacement in the Thesaurus Dialog. The built-in context menu of a word has no synonyms, but it can have them: TX Text Control lets your code extend the menu before it opens, and the spell checker creates the synonyms to put into it. The screenshots show TX Text Control Words, the Windows Forms sample application; the WPF dialog works the same way.

What the User Sees

The Thesaurus button is in the Spelling & Hyphenation group of the ribbon's Proofing tab. With a word selected, it opens the Thesaurus Dialog, and Replace puts the chosen synonym in place of the word:

TX Text Control Words: the word "powerful" is selected and the Thesaurus button of the Proofing tab is clicked, the Thesaurus Dialog lists synonyms such as almighty, all-powerful and compelling, and after Replace the sentence reads "TX Text Control is a compelling word processor."

The dialog shows:

  • Find Synonyms for, the word, which the user can change to look up another one.
  • Language, the language of the synonyms, with one entry for each language that has a thesaurus loaded.
  • Synonyms, a tree with one group per meaning. A double-click on a synonym, or the space bar, looks up the synonyms of that synonym, and the back arrow returns to the previous word.
  • Replace and Cancel. Replace, or Enter, puts the selected synonym in place of the selected text.

The Thesaurus Dialog with Find Synonyms for: powerful, Language: English (United States), and the synonym compelling selected in the tree.

When no word is selected, the dialog takes the word at the input position.

How It Works

The dialog and the ribbon button belong to TX Text Control; the synonyms come from TX Spell .NET. The spell checker connected to the TextControl through its SpellChecker property creates them from its synonym lists, the thesaurus files described in Synonym Lists. Without a connected spell checker, the dialog throws an exception.

Open the Thesaurus Dialog in Code

For an application without a ribbon, open the ThesaurusDialog from a button or a menu item of your own. It takes the TextControl in its constructor:

new TXTextControl.WPF.ThesaurusDialog(textControl1).ShowDialog();

The dialog works on the selected text of the TextControl, or on the word at the input position, which it selects. The RibbonProofingTab opens the same dialog from its Thesaurus button.

Add Synonyms to the Context Menu

TX Text Control raises TextContextMenuOpening just before it opens a context menu. The TextContextMenu property of the arguments is the menu, a ContextMenu, to which you add MenuItem objects of System.Windows.Controls. This handler adds a Synonyms submenu for the selected word, and a Thesaurus... item that opens the dialog. txSpellChecker1 is the resource of the window, which the constructor gets with FindResource, as As-You-Type Checking shows; attach the handler there too, with textControl1.TextContextMenuOpening += textControl1_TextContextMenuOpening;.

private void textControl1_TextContextMenuOpening(object sender, TXTextControl.WPF.TextContextMenuEventArgs e)
{
    // Leave the spell check menu of a misspelled word as it is
    if ((e.ContextMenuLocation & TXTextControl.ContextMenuLocation.MisspelledWord) != 0) return;

    // Offer synonyms for a single selected word
    string word = textControl1.Selection.Text.Trim();
    if (word.Length == 0 || word.Contains(" ")) return;

    // Use the language of the text, or all synonym lists when it has none
    SynonymGroup[] groups = textControl1.Selection.Culture != null
        ? txSpellChecker1.CreateSynonyms(word, textControl1.Selection.Culture)
        : txSpellChecker1.CreateSynonyms(word);

    System.Windows.Controls.MenuItem synonymsItem = new System.Windows.Controls.MenuItem { Header = "Synonyms" };

    foreach (SynonymGroup group in groups)
    {
        foreach (Synonym synonym in group.Synonyms)
        {
            System.Windows.Controls.MenuItem item = new System.Windows.Controls.MenuItem { Header = synonym.Name };

            // Replace the selected word with the synonym
            item.Click += (s, args) => textControl1.Selection.Text = synonym.Name;
            synonymsItem.Items.Add(item);
        }
    }

    System.Windows.Controls.MenuItem thesaurusItem = new System.Windows.Controls.MenuItem { Header = "Thesaurus..." };
    thesaurusItem.Click += (s, args) => new TXTextControl.WPF.ThesaurusDialog(textControl1).ShowDialog();

    e.TextContextMenu.Items.Add(new Separator());

    if (synonymsItem.Items.Count > 0)
    {
        e.TextContextMenu.Items.Add(synonymsItem);
    }

    e.TextContextMenu.Items.Add(thesaurusItem);
}

The code works as follows:

  • ContextMenuLocation says where the menu opens, as flags of ContextMenuLocation. A menu over a misspelled word is the spell check menu, which the handler leaves alone; Context Menus covers that menu.
  • The synonyms are for the selected text, Selection.Text, when it is a single word.
  • CreateSynonyms returns one SynonymGroup per meaning. With the language of the text, Selection.Culture, it uses the synonym lists of that language; text without a language uses all lists.
  • Each Synonym of a group becomes a menu item. Its Name is the synonym itself; a click writes it into the selection.
  • The labels Synonyms and Thesaurus... are the code's own; choose them to fit your application.

SynonymGroup and Synonym are types of the namespace TXTextControl.Proofing, which the code-behind needs as a using directive. MenuItem is written with its namespace, System.Windows.Controls.MenuItem, because the namespace TXTextControl.WPF.Proofing of the spell checker has a class of that name as well.

Looking Up Synonyms in Code describes CreateSynonyms and the synonym groups in detail.

Next Steps