home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / macintosh-c / macc-carbon-demos-nonbinhex.sit / macc-carbon-demos-nonbinhex / chap01-demo / SysMemRes.c < prev    next >
C/C++ Source or Header  |  2001-03-31  |  4KB  |  162 lines

  1. // *******************************************************************************************
  2. // SysMemRes.c
  3. // *******************************************************************************************
  4. //
  5. // This program:
  6. //
  7. // •    Loads a window template ('WIND') resource and creates a window.
  8. //
  9. // •    Allocates a relocatable block in the application's heap, gets its size in bytes, draws
  10. //        the size in the window, and then disposes of the block.
  11. //        
  12. // •    Loads a purgeable 'PICT' resource and a non-purgeable 'STR ' resource and draws them in
  13. //        the window.
  14. //
  15. // •    Checks if any error codes were generated as a result of calls to Memory Manager and
  16. //        Resource Manager functions.
  17. //
  18. // •    Terminates when the mouse button is clicked.
  19. //
  20. // The program utilises the following resources:
  21. //
  22. // •    A 'plst' resource.
  23. //
  24. // •    A 'WIND' resource (purgeable).
  25. //
  26. // •    A 'PICT' resource (purgeable).
  27. //
  28. // •    A 'STR ' resource (non-purgeable).
  29. //
  30. // *******************************************************************************************
  31.  
  32. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
  33.  
  34. #include <Carbon.h>
  35.  
  36. // …………………………………………………………………………………………………………………………………………………………………………………………………………………………… defines
  37.  
  38. #define rWindowResourceID        128
  39. #define rStringResourceID        128
  40. #define rPictureResourceID    128
  41.  
  42. // …………………………………………………………………………………………………………………………………………………………………………………………… function prototypes
  43.  
  44. void    main                        (void);
  45. void    doPreliminaries    (void);
  46. void    doNewWindow            (void);
  47. void    doMemory                (void);
  48. void    doResources            (void);
  49.  
  50. // ************************************************************************************** main
  51.  
  52. void  main(void)
  53. {    
  54.     doPreliminaries();
  55.     doNewWindow();
  56.     doMemory();
  57.     doResources();
  58.  
  59.     QDFlushPortBuffer(GetWindowPort(FrontWindow()),NULL);
  60.  
  61.     while(!Button())
  62.      ;
  63. }
  64.  
  65. // *************************************************************************** doPreliminaries
  66.  
  67. void  doPreliminaries(void)
  68. {
  69.     MoreMasterPointers(32);
  70.     InitCursor();
  71. }
  72.  
  73. // ******************************************************************************* doNewWindow
  74.  
  75. void  doNewWindow(void)
  76. {
  77.     WindowRef windowRef;
  78.  
  79.     windowRef = GetNewCWindow(rWindowResourceID,NULL,(WindowRef) -1);
  80.     if(windowRef == NULL)
  81.     {
  82.         SysBeep(10);
  83.         ExitToShell();
  84.     }
  85.  
  86.     SetPortWindowPort(windowRef);
  87.     UseThemeFont(kThemeSystemFont,smSystemScript);
  88. }
  89.  
  90. // ********************************************************************************** doMemory
  91.  
  92. void  doMemory(void)
  93. {
  94.     Handle    theHdl;
  95.     Size        blockSize;
  96.     OSErr        memoryError;
  97.     Str255    theString;
  98.     
  99.     theHdl = NewHandle(1024);
  100.     if(theHdl != NULL)
  101.         blockSize = GetHandleSize(theHdl);
  102.  
  103.     memoryError = MemError();
  104.     if(memoryError == noErr)
  105.     {
  106.         MoveTo(170,35);
  107.         DrawString("\pBlock Size (Bytes) = ");
  108.         NumToString(blockSize,theString);
  109.         DrawString(theString);
  110.  
  111.         MoveTo(165,55);
  112.         DrawString("\pNo Memory Manager errors");
  113.         
  114.         DisposeHandle(theHdl);
  115.     }
  116. }
  117.  
  118. // ******************************************************************************* doResources
  119.  
  120. void  doResources(void)
  121. {
  122.     PicHandle            pictureHdl;
  123.     StringHandle    stringHdl;
  124.     Rect                    pictureRect;
  125.     OSErr                    resourceError;
  126.  
  127.     pictureHdl = GetPicture(rPictureResourceID);
  128.     if(pictureHdl == NULL)
  129.     {
  130.         SysBeep(10);
  131.         ExitToShell();
  132.     }
  133.  
  134.     SetRect(&pictureRect,148,75,348,219);
  135.  
  136.     HNoPurge((Handle) pictureHdl);
  137.     DrawPicture(pictureHdl,&pictureRect);
  138.     HPurge((Handle) pictureHdl);
  139.  
  140.     stringHdl = GetString(rStringResourceID);
  141.     if(stringHdl == NULL)
  142.     {
  143.         SysBeep(10);
  144.         ExitToShell();
  145.     }
  146.  
  147.     MoveTo(103,250);
  148.     DrawString(*stringHdl);
  149.  
  150.     ReleaseResource((Handle) pictureHdl);
  151.     ReleaseResource((Handle) stringHdl);
  152.  
  153.     resourceError = ResError();
  154.     if(resourceError == noErr)
  155.     {
  156.         MoveTo(160,270);
  157.         DrawString("\pNo Resource Manager errors");
  158.     }    
  159. }
  160.  
  161. // *******************************************************************************************
  162.