home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / what's new? / sample code / human interface toolbox / packagetool / sample package / htmlsample sources / aboutbox.c next >
Encoding:
C/C++ Source or Header  |  1999-12-02  |  5.4 KB  |  175 lines

  1. /*
  2.     file AboutBox.c
  3.     
  4.     Description:
  5.     This file contains the routines used to manage the about box window
  6.     displayed when the user chooses 'About HTMLSample...' from the file menu.
  7.     
  8.     HTMLSample is an application illustrating how to use the new
  9.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  10.     is Apple's light-weight HTML rendering engine capable of
  11.     displaying HTML files.
  12.  
  13.     Copyright: © 1999 by Apple Computer, Inc.
  14.     all rights reserved.
  15.     
  16.     Disclaimer:
  17.     You may incorporate this sample code into your applications without
  18.     restriction, though the sample code has been provided "AS IS" and the
  19.     responsibility for its operation is 100% yours.  However, what you are
  20.     not permitted to do is to redistribute the source as "DSC Sample Code"
  21.     after having made changes. If you're going to re-distribute the source,
  22.     we require that you make it clear in the source that the code was
  23.     descended from Apple Sample Code, but that you've made changes.
  24.     
  25.     Change History (most recent first):
  26.     10/16/99 created
  27. */
  28.  
  29. #include "AboutBox.h"
  30.  
  31. #include <HTMLRendering.h>
  32. #include <Resources.h>
  33. #include <StdDef.h>
  34. #include <string.h>
  35. #include <Sound.h>
  36.  
  37.  
  38. enum {
  39.     kAboutBoxWindowID = 129, /* WIND resource ID for the about box. */
  40.     kMaxAboutBoxHeight = 380 /* maximum about box window height */
  41. };
  42.  
  43.     /* resource type and ID for the about box's contents */
  44. enum {
  45.     kHTMLResourceType = 'HTML',
  46.     kAboutBoxHTMLID = 128
  47. };
  48.  
  49.     /* variables used by the about box.  there is only
  50.         one about box, so these are allocated as globals */
  51. WindowPtr gAboutBox = NULL;
  52. HRReference gAboutRenderer = NULL;
  53.  
  54.  
  55. /* IsAboutBox returns true if the window pointer
  56.     in the aboutBox parameter is not NULL and
  57.     points to the about box. */
  58. Boolean IsAboutBox(WindowPtr aboutBox) {
  59.     return (aboutBox == gAboutBox && aboutBox != NULL);
  60. }
  61.  
  62.  
  63. /* OpenAboutBox opens the about box window and returns
  64.     a pointer to the window in *aboutBox.  There can only
  65.     be one about box open at a time, so if the about box is
  66.     already open, then this routine brings it to the front
  67.     by calling SelectWindow before returning a pointer to
  68.     it. */
  69. OSStatus OpenAboutBox(WindowPtr *aboutBox) {
  70.     OSStatus err;
  71.     if (gAboutBox != NULL) {
  72.             /* already showing??? bring it to the front. */
  73.         SelectWindow(gAboutBox);
  74.     } else {
  75.         Point theSize;
  76.         Handle text;
  77.         gAboutBox = NULL;
  78.         gAboutRenderer = NULL;
  79.             /* get the window */
  80.         gAboutBox = GetNewCWindow(kAboutBoxWindowID, NULL, (WindowPtr)(-1));
  81.         if (gAboutBox == NULL) { err = resNotFound; goto bail; }
  82.             /* allocate a new rendering object */
  83.         err = HRNewReference(&gAboutRenderer, kHRRendererHTML32Type, gAboutBox);
  84.         if (err != noErr) goto bail;
  85.             /* we don't have a grow box in this window */
  86.         err = HRSetGrowboxCutout(gAboutRenderer, false);
  87.         if (err != noErr) goto bail;
  88.             /* we only want a vertical scroll bar */
  89.         err = HRSetScrollbarState(gAboutRenderer, eHRScrollbarOff, eHRScrollbarAuto);
  90.         if (err != noErr) goto bail;
  91.             /* set the bounds for the rendering object */
  92.         SetPort(gAboutBox);
  93.         err = HRSetRenderingRect(gAboutRenderer, &gAboutBox->portRect);
  94.         if (err != noErr) goto bail;
  95.             /* get the HTML data we want to draw in the window */
  96.         text = GetResource(kHTMLResourceType, kAboutBoxHTMLID);
  97.         if (text == NULL) { err = resNotFound; goto bail; }
  98.             /* put the data into the rendering object */
  99.         MoveHHi(text);
  100.         HLock(text);
  101.         err = HRGoToPtr(gAboutRenderer, *text, GetHandleSize(text), false, false);
  102.         HUnlock(text);
  103.         if (err != noErr) goto bail;
  104.             /* find out the 'best' rectangle for displaying the data */
  105.         err = HRGetRenderedImageSize(gAboutRenderer, &theSize);
  106.         if (err != noErr) goto bail;
  107.             /* adjust the window's size, constraining it by the
  108.             maximum size */
  109.         if (theSize.v > kMaxAboutBoxHeight) theSize.v = kMaxAboutBoxHeight;
  110.         SizeWindow(gAboutBox, theSize.h+16, theSize.v, false);
  111.         SetPort(gAboutBox);
  112.             /* adjust the rendering object's size to the window's size */
  113.         err = HRSetRenderingRect(gAboutRenderer, &gAboutBox->portRect);
  114.         if (err != noErr) goto bail;
  115.             /* show the window */
  116.         ShowWindow(gAboutBox);
  117.     }
  118.         /* done, return the window pointer */
  119.     *aboutBox = gAboutBox;
  120.     return noErr;
  121. bail:
  122.     if (gAboutRenderer != NULL) { HRDisposeReference(gAboutRenderer); gAboutRenderer = NULL; }
  123.     if (gAboutBox) { DisposeWindow(gAboutBox); gAboutBox = NULL; }
  124.     return err;
  125. }
  126.  
  127.  
  128. /* AboutBoxCloseWindow closes the about box window. 
  129.     this routine deallocates any structures allocated
  130.     by the OpenAboutBox. */
  131. void AboutBoxCloseWindow(WindowPtr aboutBox) {
  132.     if (IsAboutBox(aboutBox)) { 
  133.         HRDisposeReference(gAboutRenderer);
  134.         gAboutRenderer = NULL;
  135.         DisposeWindow(gAboutBox);
  136.         gAboutBox = NULL;
  137.     }
  138. }
  139.  
  140.  
  141. /* EnsureAboutBoxIsClosed closes the about box window
  142.     if it is open.  If it is not open then this routine does
  143.     nothing. */
  144. void EnsureAboutBoxIsClosed(void) {
  145.     if (gAboutBox != NULL) 
  146.         AboutBoxCloseWindow(gAboutBox);
  147. }
  148.  
  149.  
  150. /* AboutBoxUpdate should be called for update events
  151.     directed at the about box window.  It calls
  152.     BeginUpdate and EndUpdate and does all of the
  153.     drawing required to refresh the about box window. */
  154. void AboutBoxUpdate(WindowPtr aboutBox) {
  155.     if (IsAboutBox(aboutBox)) {
  156.         SetPort(gAboutBox);
  157.         BeginUpdate(gAboutBox);
  158.         HRDraw(gAboutRenderer, gAboutBox->visRgn);
  159.         EndUpdate(gAboutBox);
  160.     }
  161. }
  162.  
  163.  
  164. /* AboutBoxActivate should be called for activate events
  165.     directed at the about box window. */
  166. void AboutBoxActivate(WindowPtr aboutBox, Boolean activate) {
  167.     if (IsAboutBox(aboutBox)) {
  168.         SetPort(gAboutBox);
  169.         if (activate)
  170.             HRActivate(gAboutRenderer);
  171.         else HRDeactivate(gAboutRenderer);
  172.     }
  173. }
  174.  
  175.