home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / CTableKeySingleSelector.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  6.1 KB  |  261 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 "CTableKeySingleSelector.h"
  20.  
  21. #include <LTableView.h>
  22.  
  23. #include <LCommander.h>
  24. #include <PP_KeyCodes.h>
  25.  
  26. // ---------------------------------------------------------------------------
  27. //        Ñ CTableKeySingleSelector
  28. // ---------------------------------------------------------------------------
  29.  
  30. CTableKeySingleSelector::CTableKeySingleSelector(
  31.     LTableView*        inTable)
  32.     :    LAttachment(msg_KeyPress)
  33. {
  34.     mTable = inTable;
  35. }
  36.  
  37.  
  38. // ---------------------------------------------------------------------------
  39. //        Ñ CTableKeySingleSelector
  40. // ---------------------------------------------------------------------------
  41. //    Stream constructor.
  42.  
  43. CTableKeySingleSelector::CTableKeySingleSelector(
  44.     LStream*    inStream)
  45.     :    LAttachment(inStream)
  46. {
  47.     mTable = (dynamic_cast<LTableView*>
  48.                         (LAttachable::GetDefaultAttachable()));
  49.                         
  50.     SetMessage(msg_KeyPress);
  51. }
  52.  
  53.  
  54. // ---------------------------------------------------------------------------
  55. //        Ñ ~CTableKeySingleSelector
  56. // ---------------------------------------------------------------------------
  57. //    Destructor
  58.  
  59. CTableKeySingleSelector::~CTableKeySingleSelector()
  60. {
  61. }
  62.  
  63. #pragma mark -
  64.  
  65. // ---------------------------------------------------------------------------
  66. //        Ñ ExecuteSelf
  67. // ---------------------------------------------------------------------------
  68. //    Decode the message and dispatch it.
  69.  
  70. void
  71. CTableKeySingleSelector::ExecuteSelf(
  72.     MessageT    inMessage,
  73.     void*        ioParam)
  74. {
  75.     SetExecuteHost(true);
  76.     switch (inMessage)
  77.     {
  78.         case msg_KeyPress:
  79.             HandleKeyEvent((EventRecord*) ioParam);
  80.             break;
  81.     }
  82. }
  83.  
  84. // ---------------------------------------------------------------------------
  85. //        Ñ HandleKeyEvent
  86. // ---------------------------------------------------------------------------
  87. //    Recognize and dispatch the arrow keys.
  88.  
  89. void
  90. CTableKeySingleSelector::HandleKeyEvent(
  91.     const EventRecord* inEvent)
  92. {
  93.     // Sanity check: Make sure we point to a valid table.
  94.  
  95.     if (mTable == nil)
  96.         return;
  97.  
  98.     // Prevent arrow keys from going through to host.
  99.  
  100.     SetExecuteHost(false);
  101.  
  102.     // Decode the key-down message.
  103.  
  104.     Int16 theKey = inEvent->message & charCodeMask;
  105.     
  106.     switch (theKey)
  107.     {
  108.         case char_UpArrow:
  109.             UpArrow();
  110.             break;
  111.             
  112.         case char_DownArrow:
  113.             DownArrow();
  114.             break;
  115.             
  116.         default:
  117.             SetExecuteHost(true);        // some other key, let host respond
  118.             break;
  119.     }
  120. }
  121.  
  122. #pragma mark -
  123.  
  124. // ---------------------------------------------------------------------------
  125. //        Ñ UpArrow
  126. // ---------------------------------------------------------------------------
  127. //    Select the next cell above the current cell. If no cell is selected,
  128. //    select the bottom left cell.
  129.  
  130. void
  131. CTableKeySingleSelector::UpArrow()
  132. {
  133.  
  134.     // Find first selected cell.
  135.  
  136.     STableCell theCell;
  137.     if (mTable->GetNextSelectedCell(theCell))
  138.     {
  139.         // Found a selected cell.
  140.         // If not in the first row, move up one and select.
  141.  
  142.         if (theCell.row > 1)
  143.         {
  144.             theCell.row--;
  145.             mTable->UnselectAllCells();
  146.             mTable->SelectCell(theCell);        
  147.             ScrollRowIntoFrame(theCell.row);
  148.         }
  149.     }
  150.     else
  151.     {
  152.         // Nothing selected. Start from bottom.
  153.         
  154.         TableIndexT rows, cols;
  155.         mTable->GetTableSize(rows, cols);
  156.         
  157.         if (rows > 0)
  158.         {
  159.             theCell.row = rows;
  160.             theCell.col = 1;
  161.             mTable->UnselectAllCells();
  162.             mTable->SelectCell(theCell);
  163.             ScrollRowIntoFrame(theCell.row);
  164.         }
  165.     }
  166. }
  167.  
  168.  
  169. // ---------------------------------------------------------------------------
  170. //        Ñ DownArrow
  171. // ---------------------------------------------------------------------------
  172. //    Select the next cell below the current cell. If no cell is selected,
  173. //    select the top left cell.
  174.  
  175. void
  176. CTableKeySingleSelector::DownArrow()
  177. {
  178.  
  179.     // Find first selected cell.
  180.  
  181.     TableIndexT rows, cols;
  182.     STableCell theCell;
  183.     if (!mTable->GetNextSelectedCell(theCell))
  184.     {
  185.         // Nothing selected. Start from top.
  186.  
  187.         mTable->GetTableSize(rows, cols);
  188.         if (rows > 0) {
  189.             theCell.row = 1;
  190.             theCell.col = 1;
  191.             mTable->UnselectAllCells();
  192.             mTable->SelectCell(theCell);
  193.             ScrollRowIntoFrame(theCell.row);
  194.         }
  195.     
  196.     }
  197.     else
  198.     {
  199.  
  200.         // Found a selected cell. Look for last selected cell.
  201.         
  202.         STableCell lastCell = theCell;
  203.         while (mTable->GetNextSelectedCell(theCell))
  204.         {
  205.             lastCell = theCell;
  206.         }
  207.         
  208.         // Found last selected cell.
  209.         // If not in the last row, move down one and select.
  210.         
  211.         mTable->GetTableSize(rows, cols);
  212.         
  213.         if (lastCell.row < rows)
  214.         {
  215.             lastCell.row++;
  216.             mTable->UnselectAllCells();
  217.             mTable->SelectCell(lastCell);        
  218.             ScrollRowIntoFrame(lastCell.row);
  219.         }
  220.     }
  221. }
  222.  
  223. // ---------------------------------------------------------------------------
  224. //        Ñ ScrollRowIntoFrame
  225. // ---------------------------------------------------------------------------
  226.  
  227. void
  228. CTableKeySingleSelector::ScrollRowIntoFrame(
  229.     TableIndexT        inRow)
  230. {
  231.     
  232.     // Find out how many columns are in the table.
  233.     
  234.     TableIndexT rows, cols;
  235.     mTable->GetTableSize(rows, cols);
  236.     
  237.     // Find the first seelcted cell.
  238.  
  239.     STableCell cell;
  240.     cell.row = inRow;
  241.     cell.col = 1;
  242.     
  243.     while (cell.col <= cols)
  244.     {
  245.         if (mTable->CellIsSelected(cell))
  246.         {
  247.             mTable->ScrollCellIntoFrame(cell);
  248.             return;
  249.         }
  250.         
  251.         cell.col++;
  252.     }
  253.  
  254.     // No selection shown anywhere, just choose the first cell in the row.
  255.  
  256.     cell.col = 1;
  257.     mTable->ScrollCellIntoFrame(cell);
  258.  
  259. }
  260.  
  261.