home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Sample 2.4 Think C distribution / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-29  |  8.3 KB  |  325 lines  |  [TEXT/KAHL]

  1. /*______________________________________________________________________
  2.  
  3.     help.c - Help Window Manager.
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is granted
  6.     to use this code in your own projects, provided you give credit to both
  7.     John Norstad and Northwestern University in your about box or document.
  8. _____________________________________________________________________*/
  9.  
  10.  
  11. #include "hlp.h"
  12. #include "utl.h"
  13. #include "rez.h"
  14. #include "glob.h"
  15. #include "wstm.h"
  16. #include "misc.h"
  17. #include "help.h"
  18. #include "main.h"
  19. #include "misc.h"
  20. #include "unmount.h"
  21. #include "vol.h"
  22.  
  23. #define nil 0
  24. #define normal 0        /* Think C doesn't know "normal" style. */
  25.  
  26. #define _UnmountVol    0xA00E
  27.  
  28. /*______________________________________________________________________
  29.  
  30.     Global Variables.
  31. _____________________________________________________________________*/
  32.  
  33.  
  34. static WindowPtr            HelpWindow = nil;        /* ptr to help window */
  35. static WindowRecord        HelpWRecord;            /* help window record */
  36. static WindowObject        HelpWindObject;        /* help window object */
  37.  
  38. /*______________________________________________________________________
  39.  
  40.     Click - Process Mouse Down Event.
  41.     
  42.     Entry:        where = mouse down location, local coords.
  43.                     modifiers = modifier keys.
  44. _____________________________________________________________________*/
  45.  
  46. static void Click (Point where, short modifiers)
  47.  
  48. {
  49.  
  50.     hlp_Click(where);
  51. }
  52.  
  53. /*______________________________________________________________________
  54.  
  55.     Help - Process Mouse Down Event in Help Mode.
  56.     
  57.     Entry:        where = mouse down location, local coords.
  58. _____________________________________________________________________*/
  59.  
  60.  
  61. static void Help (Point where)
  62.  
  63. {
  64.     Rect        tcRect;            /* tcon rectangle */
  65.  
  66.     hlp_GetTconRect(&tcRect);
  67.     tcRect.top -= 15;
  68.     help_Open(PtInRect(where, &tcRect) ? tagHelpTcon : tagHelpWind);
  69. }
  70.     
  71. /*______________________________________________________________________
  72.  
  73.     Close - Close Window.
  74. _____________________________________________________________________*/
  75.  
  76.  
  77. static void Close (void)
  78.  
  79. {
  80.     Prefs.helpState.moved = HelpWindObject.moved;
  81.     wstm_Save(HelpWindow, &Prefs.helpState);
  82.     Prefs.helpScrollPos = hlp_GetScrollPos();
  83.     hlp_Close();
  84.     CloseWindow(HelpWindow);
  85.     HelpWindow = nil;
  86. }
  87.     
  88. /*______________________________________________________________________
  89.  
  90.     Save - Process Save Command.
  91.     
  92.     Exit:        function result = true (nobody cares if the user canceled!)
  93. _____________________________________________________________________*/
  94.  
  95.  
  96. static Boolean Save (void)
  97.  
  98. {
  99.     Str255            prompt;            /* SFPutFile prompt string */
  100.     Str255            defName;            /* default name for help save dlog */
  101.     OSErr                rCode;            /* result code */
  102.     Str255            rCodeStr;        /* result code as a string */
  103.     long                savedTrapAddr;    /* saved UnmountVol trap adress */
  104.     
  105.     /* Get the prompt string and default file name. */
  106.     
  107.     GetIndString(prompt, strListID, abPromptStr);
  108.     GetIndString(defName, strListID, abDefNameStr);
  109.     
  110.     /* If scanning, patch the UnmountVol trap to prevent volume unmounting. */
  111.     
  112.     if (Scanning) {
  113.         savedTrapAddr = NGetTrapAddress(_UnmountVol & 0x3ff, ToolTrap);
  114.         NSetTrapAddress((long)UNMOUNT, _UnmountVol & 0x3ff, ToolTrap);
  115.     };
  116.     
  117.     /* Call hlp_Save to save the file. */
  118.     
  119.     rCode = hlp_Save(&prompt, &defName, Prefs.docCreator, MenuPick);
  120.     
  121.     /* Handle errors. */
  122.     
  123.     if (rCode) {
  124.         if (rCode == dskFulErr) {
  125.             utl_StopAlert(diskFullID, nil, 0);
  126.         } else if (rCode == fLckdErr) {
  127.             utl_StopAlert(fileLockedID, nil, 0);
  128.         } else {
  129.             NumToString(rCode, rCodeStr);
  130.             ParamText(rCodeStr, nil, nil, nil);
  131.             utl_StopAlert(unexpectedSaveID, nil, 0);
  132.         };
  133.     };
  134.     
  135.     /* If scanning, restore the UnmountVol trap. */
  136.     
  137.     if (Scanning) {
  138.         NSetTrapAddress(savedTrapAddr, _UnmountVol & 0x3ff, ToolTrap);
  139.     } else {
  140.         main_SetPort();
  141.         vol_Verify();
  142.         misc_HiliteScan();
  143.     };
  144.     
  145.     return true;
  146. }
  147.     
  148. /*______________________________________________________________________
  149.  
  150.     PageSetup - Process Page Setup Command.
  151.     
  152.     Exit:        function result = error code.
  153. _____________________________________________________________________*/
  154.  
  155.  
  156. static OSErr PageSetup (void)
  157.  
  158. {
  159.     Boolean            canceled;
  160.     
  161.     misc_ValPrint(&Prefs.helpPrint, false);
  162.     Prefs.helpPrint.menuPick = MenuPick;
  163.     return rpp_StlDlog(&Prefs.helpPrint, &canceled);
  164. }
  165.     
  166. /*______________________________________________________________________
  167.  
  168.     Print - Process Print Command.
  169.     
  170.     Entry:    printOne = true if Print One command.
  171.     
  172.     Exit:        function result = error code.
  173. _____________________________________________________________________*/
  174.  
  175.  
  176. static OSErr Print (Boolean printOne)
  177.  
  178. {
  179.     Str255            titleTmpl;    /* template for date, time, pnum in headers */
  180.     Str255            docName;        /* document name */
  181.     
  182.     GetIndString(titleTmpl, strListID, hlpDocTmpl);
  183.     GetIndString(docName, strListID, hlpDocTitle);
  184.     Prefs.helpPrint.title = (char *)docName;
  185.     Prefs.helpPrint.titleTmpl = (char *)titleTmpl;
  186.     Prefs.helpPrint.docName = (char *)docName;
  187.     misc_ValPrint(&Prefs.helpPrint, false);
  188.     Prefs.helpPrint.menuPick = MenuPick;
  189.     return hlp_Print(&Prefs.helpPrint, printOne);
  190. }
  191.     
  192. /*______________________________________________________________________
  193.  
  194.     Adjust - Adjust Menus.
  195. _____________________________________________________________________*/
  196.  
  197.  
  198. static void Adjust (void)
  199.  
  200. {
  201.     MenuHandle                fileM;                    /* handle to file menu */
  202.     MenuHandle                editM;                    /* handle to edit menu */
  203.     MenuHandle                scanM;                    /* handle to scan menu */
  204.     
  205.     fileM = GetMHandle(fileMID);
  206.     editM = GetMHandle(editMID);
  207.     scanM = GetMHandle(scanMID);
  208.     EnableItem(fileM, closeCommand);
  209.     EnableItem(fileM, saveAsCommand);
  210.     EnableItem(fileM, pageSetupCommand);
  211.     EnableItem(fileM, printCommand);
  212.     EnableItem(fileM, printOneCommand);
  213.     if (Scanning) {
  214.         DisableItem(scanM, 0);
  215.     } else {
  216.         EnableItem(scanM, 0);
  217.     };
  218.     DisableItem(editM, 0);
  219. }
  220.  
  221. /*______________________________________________________________________
  222.  
  223.     DrawTconTitle - Draw Table of Contents Title.
  224. _____________________________________________________________________*/
  225.  
  226.  
  227. static void DrawTconTitle (void)
  228.  
  229. {
  230.     Str255            str;                /* title */
  231.     short                h;                    /* horizontal coord of title */
  232.  
  233.     GetIndString(str, strListID, tconTitle);
  234.     TextFace(bold);
  235.     h = (RectList[tconRect].left + RectList[tconRect].right
  236.         - StringWidth(str)) >> 1;
  237.     MoveTo(h, RectList[tconRect].top-5);
  238.     DrawString(str);
  239.     TextFace(normal);
  240. }
  241.     
  242. /*______________________________________________________________________
  243.  
  244.     help_Open - Open Help Window.
  245.     
  246.     Entry:        tag = Tagged line to jump to, or 0 if none.
  247. _____________________________________________________________________*/
  248.  
  249.  
  250. void help_Open (short tag)
  251.  
  252. {
  253.     hlp_PBlock        p;                /* hlp_Open param block. */
  254.     short                fNum;            /* font number */
  255.     
  256.     HelpMode = false;
  257.     InitCursor();
  258.     
  259.     /* If the window is already open, activate it and jump to the tag. */
  260.     
  261.     if (HelpWindow) {
  262.         SelectWindow(HelpWindow);
  263.         if (tag) hlp_Jump(tag);
  264.         return;
  265.     };
  266.     
  267.     /* Get the help window and restore its state. */
  268.     
  269.     HelpWindow = wstm_Restore(false, helpWindID, (Ptr)&HelpWRecord,
  270.         &Prefs.helpState);
  271.     SetPort(HelpWindow);
  272.     
  273.     /* Initialize the window object. */
  274.     
  275.     ((WindowPeek)HelpWindow)->refCon = (long)&HelpWindObject;
  276.     HelpWindObject.windKind = helpWind;
  277.     HelpWindObject.moved = Prefs.helpState.moved;
  278.     SetRect(&HelpWindObject.sizeRect, HelpWindow->portRect.right, 
  279.         minHelpSize, HelpWindow->portRect.right, 0x7fff);
  280.     HelpWindObject.update = hlp_Update;
  281.     HelpWindObject.activate = hlp_Activate;
  282.     HelpWindObject.deactivate = hlp_Deactivate;
  283.     HelpWindObject.resume = nil;
  284.     HelpWindObject.suspend = nil;
  285.     HelpWindObject.click = Click;
  286.     HelpWindObject.help = Help;
  287.     HelpWindObject.grow = hlp_Grow;
  288.     HelpWindObject.zoom = hlp_Zoom;
  289.     HelpWindObject.key = hlp_Key;
  290.     HelpWindObject.close = Close;
  291.     HelpWindObject.disk = nil;
  292.     HelpWindObject.save = Save;
  293.     HelpWindObject.pageSetup = PageSetup;
  294.     HelpWindObject.print = Print;
  295.     HelpWindObject.edit = nil;
  296.     HelpWindObject.adjust = Adjust;
  297.     HelpWindObject.periodic = nil;
  298.     HelpWindObject.dialogPre = nil;
  299.     HelpWindObject.dialogPost = nil;
  300.     
  301.     /* Initialize hlp_Open param block. */
  302.     
  303.     if (!utl_GetFontNumber((Str255 *)"\pGeneva", &fNum)) fNum = applFont;
  304.     p.scrollLine = Prefs.helpScrollPos;
  305.     p.firstStrID = firstDocID;
  306.     p.listDefID = lDefID;
  307.     p.textRect = RectList[docRect];
  308.     p.fontNum = fNum;
  309.     p.fontSize = 9;
  310.     p.tabConID = tconID;
  311.     p.tabConRect = RectList[tconRect];
  312.     p.tabConFNum = fNum;
  313.     p.tabConFSize = 9;
  314.     p.tabConLSep = 12;
  315.     p.tag = tag;
  316.     p.tagRezID = tagID;
  317.     p.cellRezID = cellID;
  318.     p.cellOption = 2;
  319.     p.extraUpdate = DrawTconTitle;
  320.     
  321.     /* Call hlp_Open to complete the open. */
  322.     
  323.     hlp_Open(HelpWindow, &p);
  324. }
  325.