home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Life 1.0d2 / LifeFile.c < prev    next >
Encoding:
Text File  |  1995-07-11  |  4.1 KB  |  216 lines  |  [TEXT/MPCC]

  1. // LifeFile.c
  2. // Handle the file operation of Mike's Life
  3. // Copyright ©1995 Michael D. Crawford.  All Rights Reserved.
  4. // 11 Jun 95 Mike Crawford crawford@scruznet.com
  5. //
  6. // Revision History:
  7. // 11 Jun 95    MDC    New today
  8.  
  9. #include <QDOffScreen.h>
  10. #include "LifeFile.h"
  11. #include "Control.h"
  12. #include "LifeModules.h"
  13.  
  14. //#ifndef kCurrentISA            // I just have ThinkC 6
  15. //#include <LoMem.h>
  16. //#define LMGetMBarHeight()    MBarHeight
  17. //#endif
  18.  
  19. #define rLifeWindowID    128
  20.  
  21. WindowPtr GetLifeWindow( long height, long width, StringPtr title );
  22. Boolean GetDimensions( long *heightPtr, long *widthPtr, short *modNumPtr );
  23.  
  24. OSErr DoNewWindow( void )
  25. {
  26.     long            height;
  27.     long            width;
  28.     Boolean            isOK;
  29.     WindowPtr        win;
  30.     tLifeWorldPtr    worldPtr;
  31.     OSErr            err;
  32.     GDHandle        mainDev;
  33.     short            moduleNumber;
  34.     tLongRect        boundsRect;
  35.  
  36.  
  37.     worldPtr = NewLifeWorld();
  38.     if (!worldPtr)
  39.         return memFullErr;
  40.  
  41.     isOK = GetDimensions( &height, &width, &moduleNumber );
  42.  
  43.     // STUB We can't do more than -32767 to 32768 because of QuickDraw.  Later we'll do
  44.     // sparse matrices and so on, to increase the size.
  45.     
  46.     if ( height > 32768L )
  47.         height = 32768L;
  48.  
  49.     if ( width > 32768L )
  50.         width = 32768L;
  51.  
  52.     win = GetLifeWindow( height, width, "\pUntitled" );
  53.     if ( !win )
  54.         return memFullErr;
  55.  
  56.     worldPtr->win = win;
  57.     worldPtr->height = height;
  58.     worldPtr->width = width;    
  59.     
  60.     worldPtr->lifeModuleProcs = GetModuleProcs( moduleNumber );
  61.  
  62.     err = AllocLifeWorld( worldPtr, height, width );
  63.     if ( err ){
  64.         DisposeWindow( win );
  65.         FreeLifeWorld( worldPtr );
  66.         return err;
  67.     }
  68.  
  69.     boundsRect.left = 0;
  70.     boundsRect.top = 0;
  71.     boundsRect.right = width;
  72.     boundsRect.bottom = height;
  73.  
  74. //    ClearCells( worldPtr, &boundsRect );
  75.  
  76.     SetPort( win );
  77.  
  78.     ShowWindow( win );
  79.  
  80.     return noErr;
  81. }
  82.  
  83. void CloseLifeWindow( WindowPtr win )
  84. {
  85.     tLifeWorldPtr    worldPtr;
  86.  
  87.     worldPtr =     WinToLifeWorld( win );
  88.     if ( !worldPtr )
  89.         return;
  90. /*    
  91.     DisposeWindow( worldPtr->win );
  92.     DisposeGWorld( worldPtr->hip );
  93.     DisposeGWorld( worldPtr->hop );
  94. */    
  95.     FreeLifeWorld( worldPtr );
  96.     
  97.     return;
  98. }
  99.  
  100. WindowPtr GetLifeWindow( long height, long width, StringPtr title )
  101. {
  102.     WindowPtr    win;
  103.     GDHandle    mainDev;
  104.     Rect        r;
  105.     short        screenHeight;
  106.     short        screenWidth;
  107.     short        maxHeight;
  108.     short        maxWidth;
  109.     short        winHeight;
  110.     short        winWidth;
  111.  
  112.     win = GetNewWindow( rLifeWindowID, (WindowPtr)NULL, (WindowPtr)-1 );
  113.     if ( !win )
  114.         return (WindowPtr)NULL;
  115.  
  116.     // STUB add controls
  117.     
  118.     SetWTitle( win, title );
  119.     
  120.     // Peg to smaller of dimensions or screen size
  121.     mainDev = GetMainDevice();
  122.  
  123.     r = (*mainDev)->gdRect;
  124.  
  125.     screenHeight = r.bottom - r.top;
  126.     screenWidth = r.right - r.left;
  127.     
  128. #define kScrollBarWidth    16
  129.  
  130.     maxHeight = screenHeight - LMGetMBarHeight() - kScrollBarWidth;
  131.     maxWidth = screenWidth - kScrollBarWidth;
  132.     
  133.     winHeight = height < maxHeight? height : maxHeight;
  134.     winWidth = width < maxWidth? width : maxWidth;
  135.  
  136.     winWidth += kScrollBarWidth;
  137.     winHeight += kScrollBarWidth;
  138.     
  139.     SizeWindow( win, winWidth, winHeight, false );
  140.  
  141.     return win;
  142. }
  143.  
  144. #define rDimensionDlgID 129
  145. enum{
  146.     kDDOkButton = 1,
  147.     kDDDefUser,
  148.     kDDCancelButton,
  149.     kDDPromptTitle,
  150.     kDDHeightLabel,
  151.     kDDWidthLabel,
  152.     kDDHeightText,
  153.     kDDWidthText
  154. };
  155.  
  156. Boolean GetDimensions( long *heightPtr, long *widthPtr, short *modNumPtr )
  157. {
  158.     DialogPtr    dlg;
  159.     Boolean        result;
  160.     Str255        valStr;
  161.     Handle        h;
  162.     Rect        r;
  163.     short        kind;
  164.     short        item;
  165.     
  166.     dlg = GetNewDialog( rDimensionDlgID, (DialogPtr)NULL, (WindowPtr)-1 );
  167.     if ( !dlg )
  168.         return false;
  169.     
  170.     // STUB Set user items
  171.     // STUB Default dimensions to the main screen size, less borders
  172.     
  173.     SelIText( dlg, kDDHeightText, 0, 32000 );
  174.     
  175.     ShowWindow( dlg );
  176.     
  177.     do{
  178.         ModalDialog( (ModalFilterUPP)NULL, &item );
  179.     }while ( item != kDDOkButton && item != kDDCancelButton );
  180.         
  181.     result = false;
  182.  
  183.     if ( item == kDDOkButton ){
  184.         result = true;
  185.     
  186.         GetDItem( dlg, kDDHeightText, &kind, &h, &r );
  187.         GetIText( h, valStr );
  188.         StringToNum( valStr, heightPtr );
  189.  
  190.         GetDItem( dlg, kDDWidthText, &kind, &h, &r );
  191.         GetIText( h, valStr );
  192.         StringToNum( valStr, widthPtr );
  193.  
  194.         *modNumPtr = 0;            // STUB get this from popup menu state - note this is 0 based
  195.     }
  196.     
  197.     DisposeDialog( dlg );
  198.     
  199.     return result;
  200. }    
  201.  
  202. OSErr DoSave( void )
  203. {
  204.     WindowPtr win;
  205.     
  206.     win = FrontWindow();
  207.     
  208.     if ( !win )
  209.         return noErr;
  210.     
  211.     // STUB save the file
  212.     
  213.     return noErr;
  214. }
  215.  
  216.