home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / DTS.Lib / System6Help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  6.6 KB  |  253 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        System6Help.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "DTS.Lib2.h"
  26. #include "DTS.Lib.protos.h"
  27.  
  28. #ifndef __BALLOONS__
  29. #include <Balloons.h>
  30. #endif
  31.  
  32. #ifndef __RESOURCES__
  33. #include <Resources.h>
  34. #endif
  35.  
  36. extern HMMessageRecord    gHelpMessage;
  37. extern Rect                gHelpMessageRct;
  38. extern ControlHandle    gHelpMessageCtl;
  39. extern WindowPtr        gHelpWithWindow;
  40.  
  41. static PicHandle        gHelp6Pic;
  42.  
  43.  
  44.  
  45. /*****************************************************************************/
  46.  
  47.  
  48.  
  49. static OSErr    HelpImageDocument(FileRecHndl frHndl);
  50. static OSErr    HelpInitContent(FileRecHndl frHndl, WindowPtr window);
  51. static OSErr    HelpFreeDocument(FileRecHndl frHndl);
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56. /*****************************************************************************/
  57.  
  58.  
  59.  
  60. #pragma segment Help
  61. OSErr    HelpInitDocument(FileRecHndl frHndl)
  62. {
  63.     FileRecPtr    frPtr;
  64.  
  65.     frPtr = *frHndl;
  66.     frPtr->fileState.calcFrameRgnProc        = nil;
  67.     frPtr->fileState.contentClickProc        = nil;
  68.     frPtr->fileState.contentKeyProc          = nil;
  69.     frPtr->fileState.drawFrameProc           = nil;
  70.     frPtr->fileState.freeDocumentProc        = HelpFreeDocument;
  71.     frPtr->fileState.freeWindowProc          = nil;
  72.     frPtr->fileState.imageProc               = HelpImageDocument;
  73.     frPtr->fileState.initContentProc         = HelpInitContent;
  74.     frPtr->fileState.readDocumentProc        = nil;
  75.     frPtr->fileState.readDocumentHeaderProc  = nil;
  76.     frPtr->fileState.resizeContentProc       = nil;
  77.     frPtr->fileState.scrollFrameProc         = nil;
  78.     frPtr->fileState.undoFixupProc           = nil;
  79.     frPtr->fileState.windowCursorProc        = nil;
  80.     frPtr->fileState.writeDocumentProc       = nil;
  81.     frPtr->fileState.writeDocumentHeaderProc = nil;
  82.  
  83.     return(noErr);
  84. }
  85.  
  86.  
  87.  
  88. /*****************************************************************************/
  89. /*****************************************************************************/
  90.  
  91.  
  92.  
  93. #pragma segment Help
  94. static OSErr    HelpImageDocument(FileRecHndl frHndl)
  95. {
  96. #ifndef __MWERKS__
  97. #pragma unused (frHndl)
  98. #endif
  99.  
  100.     DoDrawControls((*frHndl)->fileState.window, false);
  101.     return(noErr);
  102. }
  103.  
  104.  
  105.  
  106. /*****************************************************************************/
  107.  
  108.  
  109.  
  110. #pragma segment TheDoc
  111. static OSErr    HelpInitContent(FileRecHndl frHndl, WindowPtr window)
  112. {
  113. #ifndef __MWERKS__
  114. #pragma unused (frHndl)
  115. #endif
  116.  
  117.     WindowPtr        oldPort;
  118.     OSErr            err;
  119.     Rect            scnRct, strRct, cntRct, ctlRct, rct, **rh;
  120.     short            xx, yy, cc;
  121.     Handle            txt;
  122.     StringHandle    sh;
  123.     StScrpHandle    stl;
  124.     ControlHandle    ctl;
  125.     TEHandle        te;
  126.     Str255            pstr;
  127.  
  128.     err = AddControlSet(window, '6hlp', kwStandardVis, 0, 0, nil);
  129.     if (err) return(err);
  130.  
  131.     scnRct = GetWindowDeviceRectNMB(gHelpWithWindow);        /* Rect of screen that window-with-help is most on. */
  132.  
  133.     if (!(ctl = CTENext(window, &te, nil, 1, false))) return(memFullErr);
  134.  
  135.     GetPort(&oldPort);
  136.     SetPort(gHelpWithWindow);
  137.     ctlRct = (*gHelpMessageCtl)->contrlRect;
  138.     LocalToGlobalRect(&ctlRct);                                /* Rect of control that is getting help. */
  139.     SetPort(oldPort);
  140.  
  141.     if (gHelp6Pic) KillPicture(gHelp6Pic);
  142.     gHelp6Pic = nil;
  143.  
  144.     pstr[0] = 0;
  145.     xx = yy = 0;
  146.  
  147.     switch (gHelpMessage.hmmHelpType) {
  148.         case khmmString:
  149.             pcpy(pstr, (StringPtr)gHelpMessage.u.hmmString);
  150.             break;
  151.         case khmmPict:
  152.             if (!(gHelp6Pic = (PicHandle)Get1Resource('PICT', gHelpMessage.u.hmmTERes))) return(ResError());
  153.             ReleaseResource((Handle)gHelp6Pic);
  154.             xx = (*gHelp6Pic)->picFrame.right  - (*gHelp6Pic)->picFrame.left;
  155.             yy = (*gHelp6Pic)->picFrame.bottom - (*gHelp6Pic)->picFrame.top;
  156.             break;
  157.         case khmmStringRes:
  158.             GetIndString(pstr, gHelpMessage.u.hmmStringRes.hmmResID, gHelpMessage.u.hmmStringRes.hmmIndex);
  159.             break;
  160.         case khmmTERes:
  161.             if (!(txt = Get1Resource('TEXT', gHelpMessage.u.hmmTERes))) return(memFullErr);
  162.             DetachResource(txt);
  163.             stl = (StScrpHandle)Get1Resource('styl', gHelpMessage.u.hmmTERes);
  164.             if (stl) DetachResource((Handle)stl);
  165.             UseControlStyle(ctl);
  166.             DisposeHandle(CTESwapText(te, txt, stl, false));
  167.             if (stl) DisposeHandle((Handle)stl);
  168.             rh = (Rect **)Get1Resource('RECT', gHelpMessage.u.hmmTERes);
  169.             if (rh) xx = (*rh)->right - (*rh)->left;
  170.             UseControlStyle(nil);
  171.             break;
  172.         case khmmSTRRes:
  173.             sh = (StringHandle)GetResource('STR ', gHelpMessage.u.hmmSTRRes);
  174.             if (sh) pcpy(pstr, *sh);
  175.             break;
  176.     }
  177.  
  178.     if (pstr[0]) {
  179.         UseControlStyle(ctl);
  180.         CTEPutPStr(ctl, pstr);
  181.         UseControlStyle(nil);
  182.     }
  183.  
  184.     SetPort(window);
  185.     if (!yy) {        /* If handling text... */
  186.         if (!xx) {
  187.             for (;;) {
  188.                 yy = CTEDocHeight(te);
  189.                 xx = (*ctl)->contrlRect.right - (*ctl)->contrlRect.left;
  190.  
  191.                 if (xx > scnRct.right - scnRct.left - 64) {
  192.                     xx = scnRct.right - scnRct.left - 64;
  193.                     break;
  194.                 }
  195.  
  196.                 if (xx > 4 * yy) break;
  197.  
  198.                 CTESize(te, (xx + xx / 2), yy, true);
  199.             }
  200.         }
  201.         yy = CTEDocHeight(te);
  202.         CTESize(te, xx, yy, true);
  203.         yy = CTEDocHeight(te);
  204.     }
  205.     SetPort(oldPort);
  206.  
  207.     MoveWindow(window, 16384, 16384, false);
  208.     ShowHide(window, true);
  209.     CleanSendBehind(window, (WindowPtr)-1);
  210.  
  211.     rct = (*ctl)->contrlRect;
  212.     SizeWindow(window, xx + rct.left, yy + 2 * rct.top, false);
  213.  
  214.     strRct = GetWindowStructureRect(window);        /* Structure rect of window-with-help. */
  215.     cntRct = GetWindowContentRect(window);            /* Content rect of window-with-help. */
  216.  
  217.     cc = (ctlRct.right + ctlRct.left) / 2;
  218.     xx = cc - xx / 2;
  219.     if (xx < (scnRct.left + 10)) xx = (scnRct.left + 10);
  220.     if (xx > scnRct.right - (cntRct.right - cntRct.left) - 10)
  221.         xx = scnRct.right - (cntRct.right - cntRct.left) - 10;
  222.  
  223.     yy = ctlRct.top - (strRct.bottom - strRct.top) - 10;
  224.     if (yy < scnRct.top + 2) {
  225.         yy = ctlRct.bottom + 10;
  226.         if (yy > scnRct.bottom - (strRct.bottom - strRct.top) - 2)
  227.             yy = scnRct.bottom - (strRct.bottom - strRct.top) - 2;
  228.     }
  229.     yy += (cntRct.top - strRct.top);
  230.  
  231.     MoveWindow(window, xx, yy, false);
  232.  
  233.     return(noErr);
  234. }
  235.  
  236.  
  237.  
  238. /*****************************************************************************/
  239.  
  240.  
  241.  
  242. #pragma segment TheDoc
  243. static OSErr    HelpFreeDocument(FileRecHndl frHndl)
  244. {
  245.     if (gHelp6Pic) KillPicture(gHelp6Pic);
  246.     gHelp6Pic = nil;
  247.  
  248.     return(DefaultFreeDocument(frHndl));
  249. }
  250.  
  251.  
  252.  
  253.