home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / packagetool / sample package / htmlsample sources / sampleutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.4 KB  |  256 lines

  1. /*
  2.     File: SampleUtils.c
  3.     
  4.     Description:
  5.         This file contains a number of utility routines used by the
  6.     HTMLSampleApplication.  These routines have been moved
  7.     here to a separate file to simplify the example.
  8.     
  9.     HTMLSample is an application illustrating how to use the new
  10.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  11.     is Apple's light-weight HTML rendering engine capable of
  12.     displaying HTML files.
  13.  
  14.     Copyright:
  15.         © Copyright 1999 Apple Computer, Inc. All rights reserved.
  16.     
  17.     Disclaimer:
  18.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  19.         ("Apple") in consideration of your agreement to the following terms, and your
  20.         use, installation, modification or redistribution of this Apple software
  21.         constitutes acceptance of these terms.  If you do not agree with these terms,
  22.         please do not use, install, modify or redistribute this Apple software.
  23.  
  24.         In consideration of your agreement to abide by the following terms, and subject
  25.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  26.         copyrights in this original Apple software (the "Apple Software"), to use,
  27.         reproduce, modify and redistribute the Apple Software, with or without
  28.         modifications, in source and/or binary forms; provided that if you redistribute
  29.         the Apple Software in its entirety and without modifications, you must retain
  30.         this notice and the following text and disclaimers in all such redistributions of
  31.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  32.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  33.         Apple Software without specific prior written permission from Apple.  Except as
  34.         expressly stated in this notice, no other rights or licenses, express or implied,
  35.         are granted by Apple herein, including but not limited to any patent rights that
  36.         may be infringed by your derivative works or by other works in which the Apple
  37.         Software may be incorporated.
  38.  
  39.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  40.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  41.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  43.         COMBINATION WITH YOUR PRODUCTS.
  44.  
  45.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  46.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  47.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  49.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  50.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  51.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52.  
  53.     Change History (most recent first):
  54.         Wed, Dec 22, 1999 -- created
  55. */
  56.  
  57.  
  58. #include "SampleUtils.h"
  59. #include <Processes.h>
  60. #include <string.h>
  61. #include <QuickDraw.h>
  62. #include <Gestalt.h>
  63. #include <Palettes.h>
  64. #include <Resources.h>
  65. #include <TextUtils.h>
  66.  
  67. #include <HTMLRendering.h>
  68.  
  69.  
  70.  
  71. /* SetWindowStandardStateSize sets the window's standard state rectangle
  72.     to a rectangle of the size suggested in the width and height parameters.
  73.     In the end, it may set the standard size to something smaller than the
  74.     width and height parameters so the entire window remains visible.  The
  75.     standard rectangle is also centered on the main screen.  The window's
  76.     standard state rectangle is used by ZoomWindow whenever when it
  77.     is called with the inZoomOut partcode. */
  78. void SetWindowStandardStateSize(WindowPtr window, short width, short height) {
  79.     Rect standard, stdRect, global, device, c, s, diffs;
  80.     RgnHandle sRgn, cRgn;
  81.     GDHandle theDevice;
  82.         /* make the window's port the current port */
  83.     SetPort(window);
  84.         /* find the window's monitor coordinates */
  85.     global = window->portRect;
  86.     LocalToGlobal((Point*) &global.top);
  87.     LocalToGlobal((Point*) &global.bottom);
  88.         /* get the maximum intersecting screen */
  89.     theDevice = GetMaxDevice(&global);
  90.     device = (**theDevice).gdRect;
  91.         /* if it's the main screen, adjust it for the menu bar height */
  92.     if (theDevice == GetMainDevice())
  93.         device.top += GetMBarHeight();
  94.         /* calculate the difference between the window's content rectangle and its frame */
  95.     sRgn = NewRgn();
  96.     cRgn = NewRgn();
  97.     GetWindowStructureRgn(window, sRgn);
  98.     GetWindowContentRgn(window, cRgn);
  99.     c = (**cRgn).rgnBBox;
  100.     s = (**sRgn).rgnBBox;
  101.     SetRect(&diffs, c.left - s.left + 2, c.top - s.top + 2, s.right - c.right + 2, s.bottom - c.bottom + 2);
  102.     DisposeRgn(sRgn);
  103.     DisposeRgn(cRgn);
  104.         /* calculate the maximum bounds for the standard rectangle */
  105.     SetRect(&standard, device.left + diffs.left, device.top + diffs.top, device.right - diffs.right, device.bottom - diffs.bottom);
  106.         /* set up our 'desired' rectangle */
  107.     SetRect(&stdRect, 0, 0, width, height);
  108.     OffsetRect(&stdRect, standard.left, standard.top);
  109.         /* fit it inside of the maximum allowable rectangle */
  110.     SectRect(&stdRect, &standard, &stdRect);
  111.         /* center it in the maximum allowable rectangle */
  112.     OffsetRect(&stdRect, (standard.right - stdRect.right)/2, (standard.bottom - stdRect.bottom)/2);
  113.         /* set the standard state rectangle */
  114.     SetWindowStandardState(window, &stdRect);
  115. }
  116.  
  117.  
  118.  
  119. /* GetApplicationFolder returns a URL referring to the folder
  120.     containing the application.  If successful, *url is set
  121.     to a new handle containing the URL.  It is the caller's
  122.     responsibility to dispose of this handle. */
  123. OSStatus GetApplicationFolderURL(Handle *url) {
  124.     OSStatus err;
  125.     FSSpec spec;
  126.     Handle theURL;
  127.         /* set up locals to a known state */
  128.     theURL = NULL;
  129.         /* find the application's folder */ 
  130.     err = GetApplicationFolder(&spec);
  131.     if (err != noErr) goto bail;
  132.         /* create a new handle for storing the URL */
  133.     theURL = NewHandle(0);
  134.     if (theURL == NULL) { err = memFullErr; goto bail; }
  135.         /* ask the HTML rendering library to convert
  136.         the FSSpec into a URL */
  137.     err = HRUtilGetURLFromFSSpec(&spec, theURL);
  138.     if (err != noErr) goto bail;
  139.     
  140.         /* add a slash at the end of the name, if one is not there */
  141.     if ( (*theURL)[GetHandleSize(theURL) - 1] != '/')
  142.         Munger(theURL, (GetHandleSize(theURL) - 1), NULL, 0, "/", 1);
  143.  
  144.         /* return the URL */
  145.     *url = theURL;
  146.     return noErr;
  147. bail:
  148.     if (theURL != NULL) DisposeHandle(theURL);
  149.     return err;
  150. }
  151.  
  152.  
  153. /* GetApplicationFolder returns the volume reference number and
  154.     directory id of the folder containing the application. */
  155. OSStatus GetApplicationFolder(FSSpec *spec) {
  156.     OSStatus err;
  157.     FCBPBRec fcpb;
  158.     Str255 name;
  159.     CInfoPBRec cat;
  160.     Handle h;
  161.     
  162.     h = GetResource('vTeZ', 0);
  163.     if (h == NULL) return resNotFound;
  164.     
  165.     memset(&fcpb, 0, sizeof(fcpb));
  166.     fcpb.ioVRefNum = 0;
  167.     fcpb.ioNamePtr = name;
  168.     fcpb.ioFCBParID = 0;
  169.     fcpb.ioRefNum = HomeResFile(h);
  170.     fcpb.ioFCBIndx = 0;
  171.     if ((err = PBGetFCBInfoSync(&fcpb)) != noErr) return err;
  172.     
  173.     memset(&cat, 0, sizeof(cat));
  174.     cat.dirInfo.ioNamePtr = name;
  175.     cat.dirInfo.ioVRefNum = fcpb.ioFCBVRefNum;
  176.     cat.dirInfo.ioFDirIndex = -1;
  177.     cat.dirInfo.ioDrDirID = fcpb.ioFCBParID;
  178.     err = PBGetCatInfoSync(&cat);
  179.     if (err != noErr) return err;
  180.     
  181.     err = FSMakeFSSpec(fcpb.ioFCBVRefNum, cat.dirInfo.ioDrParID, name, spec);
  182.     if (err != noErr) return err;
  183.     
  184.     return noErr;
  185. }
  186.  
  187. /* DrawGrowIconWithoutScrollLines draws the grow icon in the bottom
  188.     right corner of the frontmost window without drawing the scroll bar
  189.     lines.  It does this by setting the clip region to the bottom right corner
  190.     before calling DrawGrowIcon. */
  191. void DrawGrowIconWithoutScrollLines(WindowPtr window) {
  192.     RgnHandle clip;
  193.     Rect r = { 0, 0, 16, 16 };
  194.         /* set the window to the current port */
  195.     SetPort(window);
  196.         /* save the old clipping region */
  197.     clip = NewRgn();
  198.     if (clip == NULL) return;
  199.     GetClip(clip);
  200.         /* set the clipping region to the bottom right corner of the window */
  201.     OffsetRect(&r, window->portRect.right-15, window->portRect.bottom-15);
  202.     ClipRect(&r);
  203.         /* draw the grow icon */
  204.     DrawGrowIcon(window);
  205.     SetPort(window);
  206.     SetClip(clip);
  207.     DisposeRgn(clip);
  208. }
  209.  
  210.  
  211.  
  212.  
  213. /* GrayOutBox grays out an area of the screen in the current grafport.  
  214.     *theBox is in local coordinates in the current grafport. This routine
  215.     is for direct screen drawing only.  */
  216. void GrayOutBox(Rect *theBox) {
  217.     long response;
  218.     Rect globalBox;
  219.     GDHandle maxDevice;
  220.     RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF}, rgbBlack = {0, 0, 0}, sForground, sBackground;
  221.     PenState penSave;
  222.         /* save the current drawing state */
  223.     GetPenState(&penSave);
  224.         /* if no color quickdraw, fail...*/
  225.     if (Gestalt(gestaltQuickdrawVersion, &response) != noErr) response = 0;
  226.     if (response >= gestalt8BitQD) {
  227.             /* get the device for the rectangle */
  228.         globalBox = *theBox;
  229.         LocalToGlobal((Point*) &globalBox.top);
  230.         LocalToGlobal((Point*) &globalBox.bottom);
  231.         maxDevice = GetMaxDevice(&globalBox);
  232.         if (maxDevice != NULL) {
  233.                 /* calculate the best gray */
  234.             if ( GetGray(maxDevice, &rgbWhite, &rgbBlack)) {
  235.                     /* draw over the area in gray using addMax transfer mode */
  236.                 GetForeColor(&sForground);
  237.                 GetBackColor(&sBackground);
  238.                 RGBForeColor(&rgbBlack);
  239.                 RGBBackColor(&rgbWhite);
  240.                 PenMode(addMax);
  241.                 PaintRect(theBox);
  242.                 RGBForeColor(&sForground);
  243.                 RGBBackColor(&sBackground);
  244.                     /* restore the pen state and leave */
  245.                 SetPenState(&penSave);
  246.                 return;
  247.             }
  248.         }
  249.     }
  250.         /* fall through to using the gray pattern */
  251.     PenPat(&qd.gray);
  252.     PenMode(notPatBic);
  253.     PaintRect(theBox);
  254.     SetPenState(&penSave);
  255. }
  256.