home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / RAMDisk sample / Sources / RamCDev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-31  |  12.2 KB  |  449 lines  |  [TEXT/MPS ]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    RamCDev.c: Ram Disk Control Panel code
  5. **
  6. **    by Gordon Sheridan and Jim Luther
  7. **  modified by Brian Bechtel
  8. **
  9. **    File:        RamCDev.c
  10. **
  11. **    Copyright © 1992-1994 Apple Computer, Inc.
  12. **    All rights reserved.
  13. **
  14. **    You may incorporate this sample code into your applications without
  15. **    restriction, though the sample code has been provided "AS IS" and the
  16. **    responsibility for its operation is 100% yours.  However, what you are
  17. **    not permitted to do is to redistribute the source as "DTS Sample Code"
  18. **    after having made changes. If you're going to re-distribute the source,
  19. **    we require that you make it clear in the source that the code was
  20. **    descended from Apple Sample Code, but that you've made changes.
  21. **
  22. **    Change History (most recent first):
  23. **
  24. **    Change History (most recent first):
  25. **
  26. **         <4>     08/12/94    BL°B    added a very few comments
  27. **         <3>     06/10/94    BL°B    Converted to work with Metrowerks & Symantec
  28. **         <2>     6/13/93    gs        Additional Cleanup
  29. **         <1>     6/13/93    gs        Clean up format.
  30. **        <0+>     10/2/92    gs        Clean up format.
  31. **/
  32.  
  33.  
  34. #include "RamCDev.h"
  35.  
  36. ////////////////////////////////// Prototypes ///////////////////////////////////
  37.  
  38. RDataHdl  DoInit(DialogPtr CPDialog, short numItems);
  39. RDataHdl  DoNul(RDataHdl cdevGlobalsHdl);
  40. RDataHdl  DoHit(RDataHdl cdevGlobalsHdl, short item);
  41. RDataHdl     DoClose(RDataHdl cdevGlobalsHdl);
  42. RDataHdl  DoUpdate(RDataHdl cdevGlobalsHdl);
  43. RDataHdl  DoActive(RDataHdl cdevGlobalsHdl);
  44. RDataHdl  DoDeactive(RDataHdl cdevGlobalsHdl);
  45. RDataHdl  DoKeyEvent(RDataHdl cdevGlobalsHdl, DialogPtr CPDialog, EventRecord *event);
  46. void     DrawItem (RDataHdl cdevGlobalsHdl, short item);
  47. Handle     IGetHand (RDataHdl cdevGlobalsHdl, short item);
  48. void     IGetRect (RDataHdl cdevGlobalsHdl, short item, Rect *itemRect);
  49. void     IInvalidate (RDataHdl cdevGlobalsHdl, short item);
  50. short     UpdateSysz(ConfigRecHandle config);
  51.  
  52.  
  53.  
  54. /////////////////////////////// Main Entry Point ////////////////////////////////
  55.  
  56. pascal RDataHdl
  57. main (short message, short item, short numItems, short CPanelID,
  58.          EventRecord *theEvent, RDataHdl cdevGlobalsHdl, DialogPtr CPDialog)
  59. {
  60. #pragma unused (CPanelID)        /* unused formal parameters */
  61.  
  62. #ifdef THINK_C
  63.     RememberA0();
  64.     SetUpA4();
  65. #endif
  66. #ifdef __MWERKS__
  67.     long oldA4=SetCurrentA4();
  68. #endif
  69.  
  70.     if (message == macDev) return((RDataHdl) 1);                /* we work on every machine */
  71.     else if (cdevGlobalsHdl != nil)
  72.     {
  73.         switch(message)
  74.         {
  75.             case nulDev:    cdevGlobalsHdl = DoNul(cdevGlobalsHdl);
  76.                             break;
  77.                             
  78.             case initDev:    cdevGlobalsHdl = DoInit(CPDialog,numItems);
  79.                             break;
  80.                             
  81.             case closeDev:    cdevGlobalsHdl = DoClose(cdevGlobalsHdl);
  82.                             break;
  83.  
  84.             case hitDev:    cdevGlobalsHdl = DoHit(cdevGlobalsHdl,item - numItems);
  85.                             break;
  86.                             
  87.             case updateDev:    cdevGlobalsHdl = DoUpdate(cdevGlobalsHdl);
  88.                             break;
  89.             
  90.             case activDev:    cdevGlobalsHdl = DoActive(cdevGlobalsHdl);
  91.                             break;
  92.             
  93.             case deactivDev:    
  94.                             cdevGlobalsHdl = DoDeactive(cdevGlobalsHdl);
  95.                             break;
  96.             
  97.             case keyEvtDev:
  98.                             cdevGlobalsHdl = DoKeyEvent(cdevGlobalsHdl,CPDialog,theEvent);
  99.                             break;
  100.                             
  101.             case undoDev:    /* not supported */        break;
  102.             case cutDev:    DlgCut(CPDialog);        break;
  103.             case copyDev:    DlgCopy(CPDialog);        break;
  104.             case pasteDev:    DlgPaste(CPDialog);        break;
  105.             case clearDev:    DlgDelete(CPDialog);    break;
  106.             
  107.             default:        // DebugStr("\pdefault:");
  108.                             break;
  109.         }
  110.     }
  111.  
  112. #ifdef THINK_C
  113.     RestoreA4();
  114. #endif
  115. #ifdef __MWERKS__
  116.     SetA4(oldA4);
  117. #endif
  118.     return cdevGlobalsHdl;
  119. }
  120.  
  121.  
  122.  
  123. ///////////////////////////// Function Definitions //////////////////////////////
  124.  
  125. RDataHdl  DoInit(DialogPtr CPDialog, short numItems)
  126. {
  127.     short    err;
  128.     Rect    r;
  129.     short    height;
  130.     long    myBufPtr;
  131.     
  132.     RDataHdl    cdevGlobalsHdl = (RDataHdl)NewHandle(sizeof(RDataStorage));
  133.     if (cdevGlobalsHdl == nil)    return cdevGlobalsHdl;
  134.  
  135.     HLock((Handle)cdevGlobalsHdl);
  136.     (**cdevGlobalsHdl).config    = (ConfigRecHandle)Get1Resource ('RDcf', 0);
  137.     err = ResError();
  138.     if ((**cdevGlobalsHdl).config == nil)
  139.     {
  140.         if (err == resNotFound || err == 0)    // then make a new one
  141.         {
  142.             (**cdevGlobalsHdl).config = (ConfigRecHandle)NewHandle(sizeof(ConfigRec));
  143.             // check config
  144.             (**(**cdevGlobalsHdl).config).install = false;
  145.             (**(**cdevGlobalsHdl).config).size     = 0;
  146.             GetIndString((**(**cdevGlobalsHdl).config).volumeName, rStringList, rDefaultNameStr);
  147.             if (ResError() != noErr)
  148.                 BlockMove("\pRamDisk",(**(**cdevGlobalsHdl).config).volumeName,8);
  149.             AddResource((Handle)(**cdevGlobalsHdl).config, 'RDcf', 0, "\p");
  150.             // check ResError() here. You could be on a locked system disk.
  151.         }
  152.         else
  153.             DebugStr("\pGet1Resource('RDcf',0) returned nil...");
  154.     }
  155.     HLock((Handle)(**cdevGlobalsHdl).config);
  156.  
  157.     (**cdevGlobalsHdl).dlgPtr    = CPDialog;
  158.     (**cdevGlobalsHdl).dlgItems    = numItems;
  159.  
  160.     // initialize with config rsrc
  161.     SetCtlValue((ControlHandle)IGetHand(cdevGlobalsHdl,iCheckBox),
  162.                 (**(**cdevGlobalsHdl).config).install);
  163.     (**cdevGlobalsHdl).cursize = (**(**cdevGlobalsHdl).config).size;
  164.     SetIText(IGetHand(cdevGlobalsHdl,iEditText),(**(**cdevGlobalsHdl).config).volumeName);
  165.     SelIText(CPDialog, numItems + iEditText, 0, 999); /* make caret show up */
  166.  
  167.     myBufPtr = (long)LMGetBufPtr();
  168.     (**cdevGlobalsHdl).maxsize    = ( myBufPtr / (1024L*1024L)) * 1024L;
  169.     if ((**cdevGlobalsHdl).maxsize < 0)    (**cdevGlobalsHdl).maxsize = 0;
  170.     NumToString((**cdevGlobalsHdl).maxsize,(**cdevGlobalsHdl).maxStr);
  171.     (**cdevGlobalsHdl).maxStr[++(**cdevGlobalsHdl).maxStr[0]] = 'K';
  172.     
  173.     NumToString((**cdevGlobalsHdl).cursize,(**cdevGlobalsHdl).curStr);
  174.     (**cdevGlobalsHdl).curStr[++(**cdevGlobalsHdl).curStr[0]] = 'K';
  175.  
  176.     IGetRect(cdevGlobalsHdl, iSizeBar, &r);
  177.     height = r.bottom - r.top - 4;
  178.     (**cdevGlobalsHdl).sizepos    = 
  179.         (**cdevGlobalsHdl).cursize/(double)(**cdevGlobalsHdl).maxsize * height;
  180.  
  181.     (**cdevGlobalsHdl).sizePict    = GetPicture (rCDevSizeBarPICT);
  182.     if ((**cdevGlobalsHdl).sizePict == nil)
  183.         DebugStr("\pGetPicture returned nil!");
  184.         
  185.     HUnlock((Handle)(**cdevGlobalsHdl).config);
  186.     HUnlock((Handle)cdevGlobalsHdl);
  187.     
  188.     return cdevGlobalsHdl;
  189. }
  190.  
  191. RDataHdl  DoNul(RDataHdl cdevGlobalsHdl)
  192. {
  193.     HLock((Handle)cdevGlobalsHdl);
  194.     HLock((Handle)(**cdevGlobalsHdl).config);
  195.  
  196.     (**(**cdevGlobalsHdl).config).install = 
  197.             GetCtlValue((ControlHandle)IGetHand(cdevGlobalsHdl,iCheckBox));
  198.     
  199.     (**(**cdevGlobalsHdl).config).size = (**cdevGlobalsHdl).cursize;
  200.  
  201.     GetIText( IGetHand(cdevGlobalsHdl,iEditText),
  202.             (**(**cdevGlobalsHdl).config).volumeName);
  203.  
  204.     HUnlock((Handle)(**cdevGlobalsHdl).config);
  205.     HUnlock((Handle)cdevGlobalsHdl);
  206.     
  207.     return cdevGlobalsHdl;
  208. }
  209.  
  210. RDataHdl  DoHit(RDataHdl cdevGlobalsHdl, short item)
  211. {
  212.     short    value,oldSize,newSize, barHeight;
  213.     Point    mouse;
  214.     Rect    r;
  215.     
  216.     switch(item)
  217.     {
  218.         case iCheckBox:
  219.                         value = GetCtlValue((ControlHandle)IGetHand(cdevGlobalsHdl,item));
  220.                         SetCtlValue((ControlHandle)IGetHand(cdevGlobalsHdl,item),!value);
  221.                         (**(**cdevGlobalsHdl).config).install = !value;
  222.                         break;
  223.         case iSizeBar:
  224.                         oldSize = (**cdevGlobalsHdl).sizepos;
  225.                         IGetRect(cdevGlobalsHdl,iSizeBar,&r);
  226.                         barHeight = r.bottom - r.top - 4;
  227.                         while(StillDown())
  228.                         {
  229.                             GetMouse(&mouse);
  230.                             IGetRect(cdevGlobalsHdl,iSizeBar,&r);
  231.                             InsetRect(&r,0,2);
  232.                             if (PtInRect(mouse,&r))
  233.                             {
  234.                                 newSize = r.bottom - mouse.v;
  235.                                 if ((newSize < (**cdevGlobalsHdl).sizepos) ||
  236.                                     (newSize > (**cdevGlobalsHdl).sizepos))
  237.                                 {
  238.                                     (**cdevGlobalsHdl).sizepos = newSize;
  239.                                     (**cdevGlobalsHdl).cursize =
  240.                                         newSize/ (double)barHeight * 
  241.                                         (**cdevGlobalsHdl).maxsize;
  242.                                     (**cdevGlobalsHdl).cursize =
  243.                                         ((**cdevGlobalsHdl).cursize/32) * 32;
  244.                                     DrawItem(cdevGlobalsHdl,iSizeBar);
  245.                                     DrawItem(cdevGlobalsHdl,iCurSize);
  246.                                 }
  247.                             }
  248.                             else
  249.                             {
  250.                                 if ((**cdevGlobalsHdl).sizepos != oldSize)
  251.                                 {
  252.                                     (**cdevGlobalsHdl).sizepos = oldSize;
  253.                                     (**cdevGlobalsHdl).cursize =
  254.                                         oldSize / (double)barHeight * 
  255.                                         (**cdevGlobalsHdl).maxsize;
  256.                                     (**cdevGlobalsHdl).cursize =
  257.                                         ((**cdevGlobalsHdl).cursize/32) * 32;
  258.                                     DrawItem(cdevGlobalsHdl,iSizeBar);
  259.                                     DrawItem(cdevGlobalsHdl,iCurSize);
  260.                                 }
  261.                             }
  262.                         }
  263.                         break;
  264.     }
  265.     return cdevGlobalsHdl;
  266. }
  267.  
  268. RDataHdl DoClose(RDataHdl cdevGlobalsHdl)
  269. {
  270.     short    err;
  271.     
  272.     if (cdevGlobalsHdl)
  273.     {
  274.         if ( (**cdevGlobalsHdl).config)
  275.         {
  276.             err = UpdateSysz((**cdevGlobalsHdl).config);
  277.         
  278.             HLock((Handle)cdevGlobalsHdl);
  279.             ChangedResource((Handle)(**cdevGlobalsHdl).config);
  280.             WriteResource((Handle)(**cdevGlobalsHdl).config);
  281.             ReleaseResource((Handle)(**cdevGlobalsHdl).config);
  282.         // check for ResError()!!!
  283.             HUnlock((Handle)cdevGlobalsHdl);
  284.         }
  285.     
  286.         DisposHandle((Handle)cdevGlobalsHdl);
  287.         cdevGlobalsHdl = nil;
  288.     }
  289.     return cdevGlobalsHdl;
  290. }
  291.  
  292. RDataHdl  DoUpdate(RDataHdl cdevGlobalsHdl)
  293. {
  294.     DrawItem(cdevGlobalsHdl, iSizeBar);
  295.     DrawItem(cdevGlobalsHdl, iMaxSize);
  296.     DrawItem(cdevGlobalsHdl, iCurSize);
  297.     
  298.     return cdevGlobalsHdl;
  299. }
  300.  
  301. RDataHdl  DoActive(RDataHdl cdevGlobalsHdl)
  302. {
  303.     return cdevGlobalsHdl;
  304. }
  305.  
  306. RDataHdl  DoDeactive(RDataHdl cdevGlobalsHdl)
  307. {
  308.     return cdevGlobalsHdl;
  309. }
  310.  
  311. RDataHdl  DoKeyEvent(RDataHdl cdevGlobalsHdl,
  312.                  DialogPtr CPDialog,
  313.                  EventRecord *event)
  314. {
  315.     char    tempChar;
  316.     
  317.     tempChar = event->message & charCodeMask;    // get the character, and check
  318.     if (event->modifiers & cmdKey)                //  status of command key
  319.     {            
  320.         event->what = nullEvent;                // and empty event type
  321.         
  322.         switch (tempChar)                        // set appropriate message
  323.         {
  324.             case 'X':
  325.             case 'x':    DlgCut(CPDialog);    break;
  326.             
  327.             case 'C':
  328.             case 'c':    DlgCopy(CPDialog);    break;
  329.             
  330.             case 'V':
  331.             case 'v':    DlgPaste(CPDialog);    break;
  332.         }
  333.     }
  334.     return cdevGlobalsHdl;
  335. }
  336.  
  337. void  DrawItem (RDataHdl cdevGlobalsHdl, short item)
  338. {
  339.     Rect        itemRect,r;
  340.     long        len;
  341.     RgnHandle    rgn = NewRgn();
  342.     
  343.     IGetRect(cdevGlobalsHdl, item, &itemRect);
  344.     SetPort( (**cdevGlobalsHdl).dlgPtr);
  345.     GetClip(rgn);
  346.  
  347.     TextMode(srcCopy);
  348.  
  349.     switch(item)
  350.     {
  351.         case iSizeBar:
  352.                         r = itemRect;
  353.                         FrameRect(&r);
  354.                         r.bottom = r.bottom - (**cdevGlobalsHdl).sizepos;
  355.                         ClipRect(&r);
  356.                         DrawPicture((**cdevGlobalsHdl).sizePict,&itemRect);
  357.                         SetClip(rgn);
  358.                         InsetRect(&itemRect,2,2);
  359.                         itemRect.top = itemRect.bottom - (**cdevGlobalsHdl).sizepos;
  360.                         PaintRect(&itemRect);
  361.                         break;
  362.         case iMaxSize:
  363.                         NumToString((**cdevGlobalsHdl).maxsize,(**cdevGlobalsHdl).maxStr);
  364.                         (**cdevGlobalsHdl).maxStr[++(**cdevGlobalsHdl).maxStr[0]] = 'K';
  365.                         len = (**cdevGlobalsHdl).maxStr[0];
  366.                         TextBox (&(**cdevGlobalsHdl).maxStr[1], len, &itemRect, teJustRight);                        
  367.                         break;
  368.         case iCurSize:    
  369.                         NumToString((**cdevGlobalsHdl).cursize,(**cdevGlobalsHdl).curStr);
  370.                         (**cdevGlobalsHdl).curStr[++(**cdevGlobalsHdl).curStr[0]] = 'K';
  371.                         len = (**cdevGlobalsHdl).curStr[0];
  372.                         TextBox (&(**cdevGlobalsHdl).curStr[1], len, &itemRect, teJustRight);                        
  373.                         break;
  374.     }
  375.      
  376.     TextMode(srcOr);
  377. }
  378.  
  379. Handle    IGetHand (RDataHdl cdevGlobalsHdl, short item)
  380. {
  381.     Handle    itemHand;
  382.     Rect    itemRect;
  383.     short    itemType;
  384.     
  385.     GetDItem( (**cdevGlobalsHdl).dlgPtr, item + (**cdevGlobalsHdl).dlgItems,
  386.                 &itemType, &itemHand, &itemRect);
  387.     return itemHand;
  388. }
  389.  
  390. void    IGetRect (RDataHdl cdevGlobalsHdl, short item, Rect *itemRect)
  391. {
  392.     short    itemType;
  393.     Handle    itemHand;
  394.     
  395.     GetDItem( (**cdevGlobalsHdl).dlgPtr, item + (**cdevGlobalsHdl).dlgItems,
  396.                 &itemType, &itemHand, itemRect);
  397. }
  398.  
  399. void    IInvalidate (RDataHdl cdevGlobalsHdl, short item)
  400. {
  401.     Rect    itemRect;
  402.     
  403.     IGetRect(cdevGlobalsHdl, item, &itemRect);
  404.     EraseRect(&itemRect);
  405.     InvalRect(&itemRect);
  406. }
  407.  
  408. /*
  409. ** We've changed the amount of memory which is going to be allocated when we're launched.
  410. ** Now we need to change our sysz resource to reflect the new memory requirements.
  411. ** This routine gets the old sysz, (creates it if not found) and sets the appropriate
  412. ** reservation of memory.  See Inside Macintosh:Operating System Utilities, page 9-17.
  413. **/
  414. short    UpdateSysz(ConfigRecHandle config)
  415. {
  416.     Handle    sysz;
  417.     short    err = 0;
  418.     
  419.     sysz = Get1Resource ('sysz', 0);
  420.     if (sysz == nil)
  421.     {
  422.         err = ResError();
  423.         if (err == resNotFound || err == 0)    // then make a new one
  424.         {
  425.             sysz = NewHandle(sizeof(long));
  426.             if (sysz == nil)
  427.                 return -1;
  428.             
  429.             **(long **)sysz = 0;
  430.                 
  431.             AddResource(sysz, 'sysz', 0, "\p");
  432.             err =  ResError();
  433.             if (err)
  434.                 return err;
  435.         }
  436.         else
  437.             DebugStr("\pGet1Resource('sysz',0) returned nil...");
  438.     }
  439.     
  440.     if ( (**config).install)
  441.         **(long **)sysz = (16 + (**config).size) * 1024;
  442.     else
  443.         **(long **)sysz = 16 * 1024;
  444.     ChangedResource(sysz);
  445.     WriteResource(sysz);
  446.     ReleaseResource(sysz);
  447.     
  448.     return err;
  449. }