home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / EditCellDemo 1.0 / EditCellDemo.c next >
Encoding:
C/C++ Source or Header  |  1993-10-25  |  6.5 KB  |  286 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  EditCellDemo.c
  3.     
  4.     This is a simple example of editing a CTable cell inline (ie over the
  5.     cell in the table itself). This demo is not a framework around which to
  6.     develop an application, but rather a demo of the mechanics of using
  7.     a "floating" editor and using BecomeGopher().
  8.     
  9.     To run this demo
  10.     
  11.     1)    Add EditCellDemo.c and EditCellDemo.rsrc files to a new segment of 
  12.         the Showcase Application (v 1.2a or later).
  13.         
  14.     2)    Compile and Run.
  15.     
  16.     AUTHOR: Andrew_Gilmartin@Brown.Edu
  17.     MODIFIED: 93-10-25
  18.  
  19. ******************************************************************************/
  20.  
  21.  
  22. #include <CDecorator.h>
  23. #include "CSApplication.h"
  24. #include "EditCellDemo.h"
  25.  
  26.  
  27. extern CDesktop *gDesktop;
  28. extern CDecorator *gDecorator;
  29.  
  30.  
  31.  
  32. /******************************************************************************
  33.  IEditCell
  34.  
  35.     Initialize the cell editor. This is a simple subclass of CEditText with
  36.     methods to position itself over a cell and to hide and show itself 
  37.     approp.
  38. ******************************************************************************/
  39.  
  40. void CEditCell::IEditCell( CView *anEnclosure, CBureaucrat *aSupervisor )
  41. {
  42.     enum { kBorderMargin = 1, kBorderPen = 2 };
  43.     CPaneBorder *theBorder;
  44.     Rect theMargin;
  45.  
  46.         /* Init the edit text */
  47.  
  48.     IEditText
  49.         ( anEnclosure
  50.         , aSupervisor
  51.         , 0 // size will be set as needed
  52.         , 0
  53.         , 0 // position will be set as needed
  54.         , 0
  55.         , sizFIXEDSTICKY // we want the editor to scroll with the table
  56.         , sizFIXEDSTICKY
  57.         , 0 );
  58.  
  59.         /* Create a border for the editor */
  60.         
  61.     SetWholeLines( FALSE ); // Make sure the border surounds the whole cell
  62.  
  63.     theBorder = new CPaneBorder;
  64.     theBorder->IPaneBorder( kBorderFrame );
  65.     SetRect
  66.         ( &theMargin
  67.         , -kBorderMargin
  68.         , -kBorderMargin
  69.         , kBorderMargin
  70.         , kBorderMargin );
  71.     theBorder->SetPenSize( kBorderPen, kBorderPen );
  72.     theBorder->SetMargin( &theMargin );
  73.     
  74.     SetBorder( theBorder );
  75.  
  76.         /* Edit the editor until needed */
  77.         
  78.     Hide();
  79.     
  80. } /* IEditCell */
  81.  
  82.  
  83.  
  84. /******************************************************************************
  85.  PlaceCell
  86.  
  87.     Move the pane over the cell's boarders rectangle. HACK: The + 2 and + 1
  88.     offsets in the Place() command are just so the CEditText overlays the
  89.     text drawn by CTable's DrawCell().
  90. ******************************************************************************/
  91.  
  92. void CEditCell::PlaceCell( LongRect *aPlace )
  93. {
  94.     Rect delta;
  95.     
  96.         /* Place the editor over the cell */
  97.         
  98.     Place( aPlace->left + 2, aPlace->top + 1, FALSE );
  99.         
  100.         /* Size the editor to cover the cell */
  101.         
  102.     delta.top = 0;
  103.     delta.left = 0;
  104.     delta.bottom = (aPlace->bottom - aPlace->top) - height;
  105.     delta.right = (aPlace->right - aPlace->left) - width;
  106.  
  107.     ChangeSize( &delta, TRUE );
  108.  
  109. } /* PlaceCell */
  110.  
  111.  
  112.  
  113. /******************************************************************************
  114.  BecomeGopher
  115.  
  116.     Show or hide the edit pane.
  117. ******************************************************************************/
  118.  
  119. Boolean CEditCell::BecomeGopher( Boolean isBecoming )
  120. {
  121.         /* Don't change setting unless we can become the gopher */
  122.         
  123.     if ( ! inherited::BecomeGopher( isBecoming ) )
  124.         return FALSE;
  125.         
  126.         /* Show or hide the editor appropriatly */
  127.         
  128.     if ( isBecoming )
  129.     {
  130.         Show();
  131.     }
  132.     else
  133.     {
  134.         Hide();
  135.     }
  136.  
  137.     return TRUE;
  138.  
  139. } /* BecomeGopher */
  140.  
  141.  
  142.  
  143. /******************************************************************************
  144.  IEditTable
  145.  
  146.     Initilaize the edit table. This example uses one cell editor that is 
  147.     shared by all the cells.
  148. ******************************************************************************/
  149.  
  150. void CEditTable::IEditTable
  151.     ( CView *anEnclosure
  152.     , CBureaucrat *aSupervisor
  153.     , short aWidth
  154.     , short aHeight
  155.     , short aHEncl
  156.     , short aVEncl
  157.     , SizingOption aHSizing
  158.     , SizingOption aVSizing )
  159. {
  160.     ITable
  161.         ( anEnclosure, aSupervisor
  162.         , aWidth, aHeight
  163.         , aHEncl, aVEncl
  164.         , aHSizing, aVSizing );
  165.     
  166.     itsEditor = new CEditCell;
  167.     itsEditor->IEditCell( this, this );
  168.  
  169. } /* IExampleTable */
  170.  
  171.  
  172.  
  173. /******************************************************************************
  174.  DoClick
  175.  
  176.     Change editing focus to the clicked cell. NOTE: CTable's DoClick() is 
  177.     not called so its selection stuff is not used.
  178. ******************************************************************************/
  179.  
  180. void CEditTable::DoClick( Point hitPt, short modifierKeys, long when)
  181. {
  182.     LongPt pt;
  183.     Cell hitCell;
  184.  
  185.         /* Did the user click in a cell? */
  186.         
  187.     QDToFrame( hitPt, &pt );
  188.     
  189.     if ( PtInLongRect( &pt, &bounds) )
  190.     {
  191.             /* Finish editing the current cell */
  192.             
  193.         if ( itsEditor == gGopher )
  194.         {
  195.             if ( ! itsEditor->BecomeGopher( FALSE ) ) // Make sure can change focus
  196.             {
  197.                 return;
  198.             }
  199.         }
  200.         
  201.             /* Edit clicked cell */
  202.             
  203.         FindHitCell( &pt, &hitCell );
  204.         EditCell( hitCell );
  205.         
  206.         itsEditor->BecomeGopher( TRUE );
  207.     }
  208.     else
  209.     {
  210.         ClickOutsideBounds( hitPt, modifierKeys, when );
  211.     }
  212.  
  213. } /* DoClick */
  214.  
  215.  
  216.  
  217. /******************************************************************************
  218.  EditCell
  219.  
  220.     Method to move and configure the cell editor. In this case we are using
  221.     a shared cell editor, but this call could create a cell specific editor
  222.     if necessary. The only requirement would be that itsEditor be set.
  223. ******************************************************************************/
  224.  
  225. void CEditTable::EditCell( Cell editCell )
  226. {
  227.     LongRect cellRect;
  228.     Str255 cellText;
  229.     
  230.         /* Position the floating editor */
  231.         
  232.     GetCellRect( editCell, &cellRect );
  233.     itsEditor->PlaceCell( &cellRect );
  234.     
  235.         /* Initialize the floating editor's content */
  236.  
  237.     GetCellText( editCell, cellRect.right - cellRect.left, cellText );
  238.     itsEditor->SetTextString( cellText );
  239.  
  240. } /* EditCell */
  241.  
  242.  
  243.  
  244. /******************************************************************************
  245.  INewDemo
  246.  
  247.     Build the edit table demo window.
  248. ******************************************************************************/
  249.  
  250. void CEditTableDemoDir::INewDemo( CDirectorOwner *aSupervisor )
  251. {
  252.     CScrollPane* theScrollPane = NULL;
  253.     CEditTable* theTable = NULL;
  254.  
  255.     inherited::INewDemo( aSupervisor );
  256.     
  257.     itsWindow = new CWindow;
  258.     itsWindow->IWindow( 128, FALSE, gDesktop, this );
  259.  
  260.     theScrollPane = new CScrollPane;
  261.     theScrollPane->IScrollPane
  262.         ( itsWindow, this
  263.         , 0, 0
  264.         , 0, 0
  265.         , sizELASTIC, sizELASTIC
  266.         , TRUE, TRUE, TRUE );
  267.     theScrollPane->FitToEnclFrame( TRUE, TRUE );
  268.         
  269.     theTable = new CEditTable;
  270.     theTable->IEditTable
  271.         ( theScrollPane, this
  272.         , 0, 0
  273.         , 0, 0
  274.         , sizELASTIC, sizELASTIC );
  275.     theTable->FitToEnclosure( TRUE, TRUE );
  276.  
  277.     theTable->SetDefaults( 72, -1 );
  278.     theTable->AddCol( 25, -1 );
  279.     theTable->AddRow( 25, -1 );
  280.  
  281.     theScrollPane->InstallPanorama( theTable );
  282.     
  283.     gDecorator->StaggerWindow( itsWindow );
  284.     itsWindow->Select();
  285.  
  286. } /* INewDemo */