home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / Composer / CEditDictionary.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  10.9 KB  |  321 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "CEditDictionary.h"
  20. #include "CLargeEditField.h"    // msg_EditField2
  21. #include "LGAPushButton.h"
  22. #include "resgui.h"                // msg_Help
  23. #include "macutil.h"            // ShowHelp
  24. #include "CSpellChecker.h"        // ISpellChecker
  25.  
  26. // need to include "ntypes.h" before "xp_help.h" because 
  27. // "xp_help.h" doesn't include definition of MWContext though it should
  28. #include "ntypes.h"
  29. #include "xp_help.h"            // HELP_SPELL_CHECK
  30.  
  31. // If user has changed the word field, figure out the length and enable/disable appropriately
  32. void        CEditDictionary::ListenToMessage    (    MessageT inMessage, 
  33.                                                         void*        ioParam )    {
  34.                                                         
  35.     Assert_(mWordNameField);
  36.     Assert_(mAddButton);
  37.     
  38.     // This happens when user types
  39.     if (inMessage == msg_EditField2) {
  40.         if ((**(mWordNameField->GetMacTEH())).teLength) {
  41.             mAddButton->Enable();
  42.         } else {
  43.             mAddButton->Disable();
  44.         }
  45.     
  46.     // User clicked in words table    
  47.     } else if (inMessage == WordsTableID) {
  48.     
  49.         Boolean atleastoneselected = false;
  50.         // Must have at least one cell selected for remove to be valid
  51.         STableCell selchecker(0,0);
  52.         if (mWordsView->GetNextSelectedCell(selchecker)) {
  53.             atleastoneselected = true;
  54.             mRemoveButton->Enable();
  55.         } else {
  56.             mRemoveButton->Disable();
  57.         }
  58.         // Replace is enabled if there is a valid word in New Word Edit Field
  59.         // And exactly one word is selected
  60.         if ((**(mWordNameField->GetMacTEH())).teLength && atleastoneselected &&
  61.          !mWordsView->GetNextSelectedCell(selchecker) ) {
  62.             mReplaceButton->Enable();
  63.         } else {
  64.             mReplaceButton->Disable();
  65.         }
  66.     
  67.     // User left words table
  68.     } else if (inMessage == msg_LeaveDictionaryTable) {
  69.         mRemoveButton->Disable();
  70.         mReplaceButton->Disable();
  71.     
  72.     // If user clicks on Remove Button
  73.     } else if (inMessage == RemoveButtonID) {
  74.         RemoveSelectedWords();
  75.     
  76.     // If user clicks on Replace Button
  77.     } else if (inMessage == ReplaceButtonID) {
  78.         ReplaceSelectedWord();
  79.         
  80.     // If user clicks on OK Button
  81.     }    else if (inMessage == msg_OK) {
  82.         MakeDictionaryChangesPermanent();
  83.         AttemptClose();
  84.         
  85.     // If user clicks on Cancel Button
  86.     } else if (inMessage == msg_Cancel) {
  87.         AttemptClose();
  88.     
  89.     // User clicks on Help Button    
  90.     } else if (inMessage == msg_Help) {
  91.         ShowHelp(HELP_SPELL_CHECK);
  92.         
  93.     // This is the case when something important happens (selection, deselection, etc.)
  94.     // to the New Word edit field.  Check status of word
  95.     } else if (inMessage == WordID) {
  96.         if ((**(mWordNameField->GetMacTEH())).teLength) {
  97.             mAddButton->Enable();
  98.         } else {
  99.             mAddButton->Disable();
  100.         }
  101.         
  102.     // Add word to dictionary    
  103.     } else if (inMessage == AddButtonID) {
  104.         AddNewWord();
  105.     } else {
  106.         LGADialogBox::ListenToMessage(inMessage, ioParam);
  107.     }            
  108.     
  109. }
  110.  
  111. //-------------------------------------------------------------------------------------
  112. // CEditDictionary::MakeDictionaryChangesPermanent
  113. // Take words in view and set in dictionary
  114. //-------------------------------------------------------------------------------------
  115. void CEditDictionary::MakeDictionaryChangesPermanent() {
  116.  
  117.     STableCell     currCellLoc(1,1);
  118.     Str255            currCellData;
  119.     int                    maxsize = 255;
  120.     
  121.     mISpellChecker->ResetPersonalDictionary();
  122.     for (currCellLoc.row = 1; currCellLoc.row <= mNumWords; currCellLoc.row++) {
  123.         maxsize = 255;
  124.         mWordsView->GetCellData(currCellLoc, currCellData, maxsize);
  125.         mISpellChecker->AddWordToPersonalDictionary(p2cstr(currCellData));
  126.     }
  127. }
  128.  
  129.  
  130. //-------------------------------------------------------------------------------------
  131. // CEditDictionary::RemoveSelectedWord
  132. // Remove words that are currently selected
  133. //-------------------------------------------------------------------------------------
  134. void  CEditDictionary::RemoveSelectedWords() {
  135.  
  136.     STableCell currCellLoc(0,0);
  137.     int rowstodelete = 0;
  138.         
  139.     while (mWordsView->GetNextSelectedCell(currCellLoc)) {
  140.         rowstodelete++;
  141.     }
  142.     currCellLoc = mWordsView->GetFirstSelectedCell();
  143.     mWordsView->RemoveRows(rowstodelete, currCellLoc.row, true);
  144.     mNumWords-=rowstodelete;
  145. }
  146.  
  147. //-------------------------------------------------------------------------------------
  148. // CEditDictionary::ReplaceSelectedWord
  149. // Remove currently selected word with word in New Word box
  150. //-------------------------------------------------------------------------------------
  151. void  CEditDictionary::ReplaceSelectedWord() {
  152.  
  153.     STableCell currCellLoc;
  154.     Str255 theWord;
  155.     
  156.     mWordNameField->GetDescriptor(theWord);
  157.     currCellLoc = mWordsView->GetFirstSelectedCell();
  158.     if (mWordsView->IsValidCell(currCellLoc)) {
  159.         mWordsView->SetCellData(currCellLoc, theWord, theWord[0] + 1);
  160.         mWordsView->RefreshCell(currCellLoc);
  161.     }
  162. }
  163.  
  164.  
  165. //-------------------------------------------------------------------------------------
  166. // CEditDictionary::AddNewWord
  167. // Add the word in "New Word" edit field to personal dictionary
  168. //-------------------------------------------------------------------------------------
  169. void  CEditDictionary::AddNewWord()    {
  170.  
  171.     Str255 theWord;
  172.     Str255 currWord;
  173.     Assert_(mWordNameField);
  174.     Assert_(mISpellChecker);
  175.     
  176.     STableCell cell(1,1);
  177.     Uint32 maxbytes;
  178.  
  179.     mWordNameField->GetDescriptor(theWord);
  180.     
  181.     /*
  182.     Check to make sure word is not already in the list.  If it is, then highlight that word
  183.     and return so we don't insert.
  184.     */
  185.     for (cell.row = 1; cell.row <= mNumWords; cell.row++) {
  186.         maxbytes = 255;
  187.         mWordsView->GetCellData(cell, currWord, maxbytes);
  188.         if (currWord[0] == theWord[0] && 
  189.          !strncmp(reinterpret_cast<const char *>(&(currWord[1])), reinterpret_cast<const char *>(&(theWord[1])), theWord[0]) ) {        // If word already exists exit
  190.                 mWordsView->UnselectAllCells();
  191.                 mWordsView->SelectCell(cell);
  192.                 return;
  193.         }
  194.     }
  195.     
  196.     mWordsView->InsertRows(1, mNumWords, theWord, theWord[0] + 1, Refresh_Yes);
  197.     mNumWords++;
  198.     cell.row = mNumWords;
  199.     mWordsView->UnselectAllCells();
  200.     mWordsView->SelectCell(cell);
  201. }
  202.  
  203.  
  204. //-------------------------------------------------------------------------------------
  205. // CEditDictionary::FinishCreateSelf
  206. // Setup object references
  207. //-------------------------------------------------------------------------------------
  208. void    CEditDictionary::FinishCreateSelf ()    {
  209.  
  210.     mAddButton = dynamic_cast<LGAPushButton *>(FindPaneByID(AddButtonID));
  211.     mReplaceButton = dynamic_cast<LGAPushButton *>(FindPaneByID(ReplaceButtonID));
  212.     mRemoveButton = dynamic_cast<LGAPushButton *>(FindPaneByID(RemoveButtonID));
  213.     mHelpButton = dynamic_cast<LGAPushButton *>(FindPaneByID(HelpButtonID));
  214.     mWordNameField = dynamic_cast<CLargeEditFieldBroadcast *>(FindPaneByID(WordID));
  215.     mWordsView = dynamic_cast<CTextTable *>(FindPaneByID( WordsTableID ));
  216.     
  217.     ThrowIfNil_(mAddButton);
  218.     ThrowIfNil_(mReplaceButton);
  219.     ThrowIfNil_(mRemoveButton);
  220.     ThrowIfNil_(mHelpButton);
  221.     ThrowIfNil_(mWordNameField);
  222.     ThrowIfNil_(mWordsView);
  223.         
  224.     LGADialogBox::FinishCreateSelf();
  225.  
  226.     PenState penState;
  227.     ::GetPenState( &penState );
  228.     mWordsView->AddAttachment( new LColorEraseAttachment( &penState, NULL, NULL, true ) );
  229.     mAddButton->Disable();        // Add button should be originally disabled
  230.     mReplaceButton->Disable();
  231.     mRemoveButton->Disable();
  232.     
  233.     mAddButton->AddListener( this );
  234.     mReplaceButton->AddListener( this );
  235.     mRemoveButton->AddListener( this );
  236.     mWordNameField->AddListener( this );
  237.     mWordsView->AddListener( this );
  238.     mHelpButton->AddListener( this );
  239. }
  240.  
  241. // Constructor
  242.  
  243. CEditDictionary::CEditDictionary(LStream *inStream)    : mNumWords(0), 
  244.         mWordNameField(NULL), mISpellChecker(NULL), mAddButton(NULL), mWordsView(NULL), 
  245.         mHelpButton(NULL), LGADialogBox (inStream) {
  246.  
  247. }
  248.  
  249. //-------------------------------------------------------------------------------------
  250. // CEditDictionary::SetISPellChecker
  251. // Sets personal dictionary and inserts into GUI
  252. //-------------------------------------------------------------------------------------
  253.  
  254. void CEditDictionary::SetISpellChecker(ISpellChecker *i)    {
  255.  
  256.     STableCell    cell(0, 1);
  257.     int unusedNumber;
  258.     char word_buffer[max_word_length];
  259.     int curr_word_length;
  260.     
  261.     mISpellChecker = i;
  262.     cell.row = 0;
  263.     
  264.     if ((mISpellChecker->GetFirstPersonalDictionaryWord(word_buffer, max_word_length) >= 0) && mWordsView) {
  265.         mWordsView->FocusDraw();
  266.         do    {
  267.             mNumWords++;
  268.             cell.row++;
  269.             curr_word_length = strlen(word_buffer);
  270.             CStr255 pascalstring( word_buffer );
  271.             mWordsView->InsertRows(1, cell.row-1, NULL ,0, Refresh_Yes);
  272.             mWordsView->SetCellData(cell, pascalstring, curr_word_length + 1);
  273.             // Insert the words into dialog box here!
  274.         }    while    (mISpellChecker->GetNextPersonalDictionaryWord(word_buffer, max_word_length) >= 0);
  275.     }
  276.     
  277.     mWordsView->GetTableSize(mNumWords, unusedNumber);
  278. }
  279.  
  280. //-------------------------------------------------------------------------------------
  281. // CEditDictionary::GetISPellChecker
  282. // Get the Personal Dictionary
  283. //-------------------------------------------------------------------------------------
  284. ISpellChecker *CEditDictionary::GetISpellChecker()    {
  285.  
  286.     return mISpellChecker;
  287. }
  288.  
  289. //-------------------------------------------------------------------------------------
  290. // CEditDictionaryTable::BeTarget()
  291. // Broadcast messages when we become target so we can setup buttons
  292. //-------------------------------------------------------------------------------------
  293. void CEditDictionaryTable::BeTarget() {
  294.  
  295.     CTextTable::BeTarget();
  296.     BroadcastMessage(CEditDictionary::WordsTableID, NULL);
  297. }
  298.  
  299. //-------------------------------------------------------------------------------------
  300. // CEditDictionaryTable::DontBeTarget()
  301. // Broadcast messages when we lose target so we can setup buttons
  302. //-------------------------------------------------------------------------------------
  303. void CEditDictionaryTable::DontBeTarget() {
  304.  
  305.     CTextTable::DontBeTarget();
  306.     BroadcastMessage(CEditDictionary::msg_LeaveDictionaryTable, NULL);
  307. }
  308.  
  309. //-------------------------------------------------------------------------------------
  310. // HiliteSelection
  311. // Setup back/front colors.  Otherwise we sometimes get the de-highlighting
  312. // wrong when switching selection
  313. //-------------------------------------------------------------------------------------
  314. void CEditDictionaryTable::HiliteSelection(    Boolean    inActively,     Boolean     inHilite )
  315. {
  316.     if (inActively) {
  317.         StColorPenState::Normalize();
  318.     }
  319.     LTableView::HiliteSelection(     inActively, inHilite );
  320. }
  321.