home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / editor / EdSpellCheck.js < prev    next >
Text File  |  2003-06-08  |  15KB  |  537 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. /*
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released
  14.  * March 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape
  17.  * Communications Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Charles Manske (cmanske@netscape.com)
  23.  *   Neil Rashbrook (neil@parkwaycc.co.uk)
  24.  */
  25.  
  26. var gMisspelledWord;
  27. var gSpellChecker = null;
  28. var gAllowSelectWord = true;
  29. var gPreviousReplaceWord = "";
  30. var gFirstTime = true;
  31.  
  32. function Startup()
  33. {
  34.   var sendMailMessageMode = false;
  35.  
  36.   if (!GetCurrentEditor())
  37.   {
  38.     window.close();
  39.     return;
  40.   }
  41.  
  42.   // Get the spellChecker shell
  43.   gSpellChecker = Components.classes['@mozilla.org/editor/editorspellchecker;1'].createInstance(Components.interfaces.nsIEditorSpellCheck);
  44.   if (!gSpellChecker)
  45.   {
  46.     dump("SpellChecker not found!!!\n");
  47.     window.close();
  48.     return;
  49.   }
  50.  
  51.   // Start the spell checker module.
  52.   try {
  53.     // TxtSrv Filter Contract Id
  54.     var filterContractId;
  55.     sendMailMessageMode = window.arguments[0];
  56.     var skipBlockQuotes = window.arguments[1];
  57.  
  58.     if (skipBlockQuotes)
  59.       filterContractId = "@mozilla.org/editor/txtsrvfiltermail;1";
  60.     else
  61.       filterContractId = "@mozilla.org/editor/txtsrvfilter;1";
  62.  
  63.     gSpellChecker.setFilter(Components.classes[filterContractId].createInstance(Components.interfaces.nsITextServicesFilter));
  64.     gSpellChecker.InitSpellChecker(GetCurrentEditor());
  65.  
  66.    // XXX: We need to read in a pref here so we can set the
  67.    //      default language for the spellchecker!
  68.    // gSpellChecker.SetCurrentDictionary();
  69.   }
  70.   catch(ex) {
  71.    dump("*** Exception error: InitSpellChecker\n");
  72.     window.close();
  73.     return;
  74.   }
  75.  
  76.   gDialog.MisspelledWordLabel = document.getElementById("MisspelledWordLabel");
  77.   gDialog.MisspelledWord      = document.getElementById("MisspelledWord");
  78.   gDialog.ReplaceButton       = document.getElementById("Replace");
  79.   gDialog.IgnoreButton        = document.getElementById("Ignore");
  80.   gDialog.StopButton          = document.getElementById("Stop");
  81.   gDialog.CloseButton         = document.getElementById("Close");
  82.   gDialog.ReplaceWordInput    = document.getElementById("ReplaceWordInput");
  83.   gDialog.SuggestedList       = document.getElementById("SuggestedList");
  84.   gDialog.LanguageMenulist    = document.getElementById("LanguageMenulist");
  85.  
  86.   // Fill in the language menulist and sync it up
  87.   // with the spellchecker's current language.
  88.  
  89.   var curLang;
  90.  
  91.   try {
  92.     curLang = gSpellChecker.GetCurrentDictionary();
  93.   } catch(ex) {
  94.     curLang = "";
  95.   }
  96.  
  97.   InitLanguageMenu(curLang);
  98.   
  99.   // Get the first misspelled word and setup all UI
  100.   NextWord();
  101.  
  102.   // When startup param is true, setup different UI when spell checking 
  103.   //   just before sending mail message  
  104.   if (sendMailMessageMode)
  105.   {
  106.     // If no misspelled words found, simply close dialog and send message
  107.     if (!gMisspelledWord)
  108.     {
  109.       onClose();
  110.       return;
  111.     }
  112.  
  113.     // Hide "Close" button and use "Send" instead
  114.     gDialog.CloseButton.hidden = true;
  115.     gDialog.CloseButton = document.getElementById("Send");
  116.     gDialog.CloseButton.hidden = false;
  117.   }
  118.   else
  119.   {
  120.     // Normal spell checking - hide the "Stop" button
  121.     // (Note that this button is the "Cancel" button for
  122.     //  Esc keybinding and related window close actions)
  123.     gDialog.StopButton.hidden = true;
  124.   }
  125.  
  126.   // Clear flag that determines message when
  127.   //  no misspelled word is found
  128.   //  (different message when used for the first time)
  129.   gFirstTime = false;
  130. }
  131.  
  132. function InitLanguageMenu(curLang)
  133. {
  134.  
  135.   var o1 = {};
  136.   var o2 = {};
  137.  
  138.   // Get the list of dictionaries from
  139.   // the spellchecker.
  140.  
  141.   try {
  142.     gSpellChecker.GetDictionaryList(o1, o2);
  143.   } catch(ex) {
  144.     dump("Failed to get DictionaryList!\n");
  145.     return;
  146.   }
  147.  
  148.   var dictList = o1.value;
  149.   var count    = o2.value;
  150.  
  151.   // Load the string bundles that will help us map
  152.   // RFC 1766 strings to UI strings.
  153.  
  154.   // Load the language string bundle.
  155.   var languageBundle = document.getElementById("languageBundle");
  156.   var regionBundle;
  157.   // If we have a language string bundle, load the region string bundle.
  158.   if (languageBundle)
  159.     regionBundle = document.getElementById("regionBundle");
  160.   
  161.   var menuStr2;
  162.   var isoStrArray;
  163.   var defaultItem = null;
  164.   var langId;
  165.   var i;
  166.   for (i = 0; i < dictList.length; i++)
  167.   {
  168.     try {
  169.       langId = dictList[i];
  170.       isoStrArray = dictList[i].split("-");
  171.  
  172.       dictList[i] = new Array(2); // first subarray element - pretty name
  173.       dictList[i][1] = langId;    // second subarray element - language ID
  174.  
  175.       if (languageBundle && isoStrArray[0])
  176.         dictList[i][0] = languageBundle.getString(isoStrArray[0].toLowerCase());
  177.  
  178.       if (regionBundle && dictList[i][0] && isoStrArray.length > 1 && isoStrArray[1])
  179.       {
  180.         menuStr2 = regionBundle.getString(isoStrArray[1].toLowerCase());
  181.         if (menuStr2)
  182.           dictList[i][0] = dictList[i][0] + "/" + menuStr2;
  183.       }
  184.  
  185.       if (!dictList[i][0])
  186.         dictList[i][0] = dictList[i][1];
  187.     } catch (ex) {
  188.       // GetString throws an exception when
  189.       // a key is not found in the bundle. In that
  190.       // case, just use the original dictList string.
  191.  
  192.       dictList[i][0] = dictList[i][1];
  193.     }
  194.   }
  195.   
  196.   // note this is not locale-aware collation, just simple ASCII-based sorting
  197.   // we really need to add loacel-aware JS collation, see bug XXXXX
  198.   dictList.sort();
  199.  
  200.   for (i = 0; i < dictList.length; i++)
  201.   {
  202.     var item = gDialog.LanguageMenulist.appendItem(dictList[i][0], dictList[i][1]);
  203.     if (curLang && dictList[i][1] == curLang)
  204.       defaultItem = item;
  205.   }
  206.  
  207.   // Now make sure the correct item in the menu list is selected.
  208.  
  209.   if (defaultItem)
  210.     gDialog.LanguageMenulist.selectedItem = defaultItem;
  211. }
  212.  
  213. function DoEnabling()
  214. {
  215.   if (!gMisspelledWord)
  216.   {
  217.     // No more misspelled words
  218.     gDialog.MisspelledWord.setAttribute("value",GetString( gFirstTime ? "NoMisspelledWord" : "CheckSpellingDone"));
  219.  
  220.     gDialog.ReplaceButton.removeAttribute("default");
  221.     gDialog.IgnoreButton.removeAttribute("default");
  222.  
  223.     gDialog.CloseButton.setAttribute("default","true");
  224.     // Shouldn't have to do this if "default" is true?
  225.     gDialog.CloseButton.focus();
  226.  
  227.     SetElementEnabledById("MisspelledWordLabel", false);
  228.     SetElementEnabledById("ReplaceWordLabel", false);
  229.     SetElementEnabledById("ReplaceWordInput", false);
  230.     SetElementEnabledById("CheckWord", false);
  231.     SetElementEnabledById("SuggestedListLabel", false);
  232.     SetElementEnabledById("SuggestedList", false);
  233.     SetElementEnabledById("Ignore", false);
  234.     SetElementEnabledById("IgnoreAll", false);
  235.     SetElementEnabledById("Replace", false);
  236.     SetElementEnabledById("ReplaceAll", false);
  237.     SetElementEnabledById("AddToDictionary", false);
  238.   } else {
  239.     SetElementEnabledById("MisspelledWordLabel", true);
  240.     SetElementEnabledById("ReplaceWordLabel", true);
  241.     SetElementEnabledById("ReplaceWordInput", true);
  242.     SetElementEnabledById("CheckWord", true);
  243.     SetElementEnabledById("SuggestedListLabel", true);
  244.     SetElementEnabledById("SuggestedList", true);
  245.     SetElementEnabledById("Ignore", true);
  246.     SetElementEnabledById("IgnoreAll", true);
  247.     SetElementEnabledById("AddToDictionary", true);
  248.  
  249.     gDialog.CloseButton.removeAttribute("default");
  250.     SetReplaceEnable();
  251.   }
  252. }
  253.  
  254. function NextWord()
  255. {
  256.   gMisspelledWord = gSpellChecker.GetNextMisspelledWord();
  257.   SetWidgetsForMisspelledWord();
  258. }
  259.  
  260. function SetWidgetsForMisspelledWord()
  261. {
  262.   gDialog.MisspelledWord.setAttribute("value", TruncateStringAtWordEnd(gMisspelledWord, 30, true));
  263.  
  264.  
  265.   // Initial replace word is misspelled word
  266.   gDialog.ReplaceWordInput.value = gMisspelledWord;
  267.   gPreviousReplaceWord = gMisspelledWord;
  268.  
  269.   // This sets gDialog.ReplaceWordInput to first suggested word in list
  270.   FillSuggestedList(gMisspelledWord);
  271.  
  272.   DoEnabling();
  273.  
  274.   if (gMisspelledWord)
  275.     SetTextboxFocus(gDialog.ReplaceWordInput);
  276. }
  277.  
  278. function CheckWord()
  279. {
  280.   word = gDialog.ReplaceWordInput.value;
  281.   if (word) 
  282.   {
  283.     if (gSpellChecker.CheckCurrentWord(word))
  284.     {
  285.       FillSuggestedList(word);
  286.       SetReplaceEnable();
  287.     } 
  288.     else 
  289.     {
  290.       ClearListbox(gDialog.SuggestedList);
  291.       var item = gDialog.SuggestedList.appendItem(GetString("CorrectSpelling"), "");
  292.       if (item) item.setAttribute("disabled", "true");
  293.       // Suppress being able to select the message text
  294.       gAllowSelectWord = false;
  295.     }
  296.   }
  297. }
  298.  
  299. function SelectSuggestedWord()
  300. {
  301.   if (gAllowSelectWord)
  302.   {
  303.     var selectedItem
  304.     if (gDialog.SuggestedList.selectedItem)
  305.     {
  306.       var selValue = gDialog.SuggestedList.selectedItem.getAttribute("label");
  307.       gDialog.ReplaceWordInput.value = selValue;
  308.       gPreviousReplaceWord = selValue;
  309.     }
  310.     else
  311.     {
  312.       gDialog.ReplaceWordInput.value = gPreviousReplaceWord;
  313.     }
  314.     SetReplaceEnable();
  315.   }
  316. }
  317.  
  318. function ChangeReplaceWord()
  319. {
  320.   // Calling this triggers SelectSuggestedWord(),
  321.   //  so temporarily suppress the effect of that
  322.   var saveAllow = gAllowSelectWord;
  323.   gAllowSelectWord = false;
  324.  
  325.   // Select matching word in list
  326.   var newIndex = -1;
  327.   var newSelectedItem;
  328.   var replaceWord = TrimString(gDialog.ReplaceWordInput.value);
  329.   if (replaceWord)
  330.   {
  331.     for (var i = 0; i < gDialog.SuggestedList.getRowCount(); i++)
  332.     {
  333.       var item = gDialog.SuggestedList.getItemAtIndex(i);
  334.       if (item.getAttribute("label") == replaceWord)
  335.       {
  336.         newSelectedItem = item;
  337.         break;
  338.       }
  339.     }
  340.   }
  341.   gDialog.SuggestedList.selectedItem = newSelectedItem;
  342.  
  343.   gAllowSelectWord = saveAllow;
  344.  
  345.   // Remember the new word
  346.   gPreviousReplaceWord = gDialog.ReplaceWordInput.value;
  347.  
  348.   SetReplaceEnable();
  349. }
  350.  
  351. function Ignore()
  352. {
  353.   NextWord();
  354. }
  355.  
  356. function IgnoreAll()
  357. {
  358.   if (gMisspelledWord) {
  359.     gSpellChecker.IgnoreWordAllOccurrences(gMisspelledWord);
  360.   }
  361.   NextWord();
  362. }
  363.  
  364. function Replace()
  365. {
  366.   var newWord = gDialog.ReplaceWordInput.value;
  367.   if (gMisspelledWord && gMisspelledWord != newWord)
  368.   {
  369.     var editor = GetCurrentEditor();
  370.     editor.beginTransaction();
  371.     try {
  372.       gSpellChecker.ReplaceWord(gMisspelledWord, newWord, false);
  373.     } catch (e) {}
  374.     editor.endTransaction();
  375.   }
  376.   NextWord();
  377. }
  378.  
  379. function ReplaceAll()
  380. {
  381.   var newWord = gDialog.ReplaceWordInput.value;
  382.   if (gMisspelledWord && gMisspelledWord != newWord)
  383.   {
  384.     var editor = GetCurrentEditor();
  385.     editor.beginTransaction();
  386.     try {
  387.       gSpellChecker.ReplaceWord(gMisspelledWord, newWord, true);
  388.     } catch (e) {}
  389.     editor.endTransaction();
  390.   }
  391.   NextWord();
  392. }
  393.  
  394. function AddToDictionary()
  395. {
  396.   if (gMisspelledWord) {
  397.     gSpellChecker.AddWordToDictionary(gMisspelledWord);
  398.   }
  399.   NextWord();
  400. }
  401.  
  402. function EditDictionary()
  403. {
  404.   window.openDialog("chrome://editor/content/EdDictionary.xul", "_blank", "chrome,close,titlebar,modal", "", gMisspelledWord);
  405. }
  406.  
  407. function SelectLanguage()
  408. {
  409.   try {
  410.     var item = gDialog.LanguageMenulist.selectedItem;
  411.     if (item.value != "more-cmd")
  412.       gSpellChecker.SetCurrentDictionary(item.value);
  413.     else
  414.       window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no,modal", xlateURL('urn:clienturl:composer:spellcheckers'));
  415.   } catch (ex) {
  416.     dump(ex);
  417.   }
  418. }
  419.  
  420. function Recheck()
  421. {
  422.   //TODO: Should we bother to add a "Recheck" method to interface?
  423.   try {
  424.     var curLang = gSpellChecker.GetCurrentDictionary();
  425.  
  426.     gSpellChecker.UninitSpellChecker();
  427.     gSpellChecker.InitSpellChecker(GetCurrentEditor());
  428.     gSpellChecker.SetCurrentDictionary(curLang);
  429.     gMisspelledWord = gSpellChecker.GetNextMisspelledWord();
  430.     SetWidgetsForMisspelledWord();
  431.   } catch(ex) {
  432.     dump(ex);
  433.   }
  434. }
  435.  
  436. function FillSuggestedList(misspelledWord)
  437. {
  438.   var list = gDialog.SuggestedList;
  439.  
  440.   // Clear the current contents of the list
  441.   gAllowSelectWord = false;
  442.   ClearListbox(list);
  443.   var item;
  444.  
  445.   if (misspelledWord.length > 0)
  446.   {
  447.     // Get suggested words until an empty string is returned
  448.     var count = 0;
  449.     var firstWord = 0;
  450.     do {
  451.       var word = gSpellChecker.GetSuggestedWord();
  452.       if (count==0)
  453.         firstWord = word;
  454.       if (word.length > 0)
  455.       {
  456.         list.appendItem(word, "");
  457.         count++;
  458.       }
  459.     } while (word.length > 0);
  460.  
  461.     if (count == 0)
  462.     {
  463.       // No suggestions - show a message but don't let user select it
  464.       item = list.appendItem(GetString("NoSuggestedWords"));
  465.       if (item) item.setAttribute("disabled", "true");
  466.       gAllowSelectWord = false;
  467.     } else {
  468.       gAllowSelectWord = true;
  469.       // Initialize with first suggested list by selecting it
  470.       gDialog.SuggestedList.selectedIndex = 0;
  471.     }
  472.   } 
  473.   else
  474.   {
  475.     item = list.appendItem("", "");
  476.     if (item)
  477.       item.setAttribute("disabled", "true");
  478.   }
  479. }
  480.  
  481. function SetReplaceEnable()
  482. {
  483.   // Enable "Change..." buttons only if new word is different than misspelled
  484.   var newWord = gDialog.ReplaceWordInput.value;
  485.   var enable = newWord.length > 0 && newWord != gMisspelledWord;
  486.   SetElementEnabledById("Replace", enable);
  487.   SetElementEnabledById("ReplaceAll", enable);
  488.   if (enable)
  489.   {
  490.     gDialog.ReplaceButton.setAttribute("default","true");
  491.     gDialog.IgnoreButton.removeAttribute("default");
  492.   }
  493.   else
  494.   {
  495.     gDialog.IgnoreButton.setAttribute("default","true");
  496.     gDialog.ReplaceButton.removeAttribute("default");
  497.   }
  498. }
  499.  
  500. function doDefault()
  501. {
  502.   if (gDialog.ReplaceButton.getAttribute("default") == "true")
  503.     Replace();
  504.   else if (gDialog.IgnoreButton.getAttribute("default") == "true")
  505.     Ignore();
  506.   else if (gDialog.CloseButton.getAttribute("default") == "true")
  507.     onClose();
  508.  
  509.   return false;
  510. }
  511.  
  512. function CancelSpellCheck()
  513. {
  514.   if (gSpellChecker)
  515.   {
  516.     try {
  517.       gSpellChecker.UninitSpellChecker();
  518.     } finally { gSpellChecker = null; }
  519.   }
  520.  
  521.   // Signal to calling window that we canceled
  522.   window.opener.cancelSendMessage = true;
  523.   return true;
  524. }
  525.  
  526. function onClose()
  527. {
  528.   if (gSpellChecker)
  529.   {
  530.     try {
  531.       gSpellChecker.UninitSpellChecker();
  532.     } finally { gSpellChecker = null; }
  533.   }
  534.   window.opener.cancelSendMessage = false;
  535.   window.close();
  536. }
  537.