home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / AOCE Utilities / AOCE Snippits / SnippitTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-25  |  11.0 KB  |  565 lines  |  [TEXT/KAHL]

  1. /*
  2.  * SnippitTest.c
  3.  * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
  4.  * Programmed by Martin Minow,
  5.  *    Internet:    minow@apple.com
  6.  *    AppleLink:    MINOW
  7.  * Ok, let's get one thing straight. We don't recommend that
  8.  * you write applications in this sloppy fashion.
  9.  */
  10. #include <OCE.h>
  11. #include <OCEAuthDir.h>
  12. #include <OCEErrors.h>
  13. #include <GestaltEqu.h>
  14. #include <Dialogs.h>
  15. #include <ToolUtils.h>
  16. #include <Resources.h>
  17. #include <Menus.h>
  18. #include <Errors.h>
  19. #include <Fonts.h>
  20. #include <Desk.h>
  21. #include <OSEvents.h>
  22. #include <Packages.h>
  23.  
  24. #ifndef FALSE
  25. #define FALSE    0
  26. #define TRUE    1
  27. #endif
  28.  
  29. WindowPtr            gWindowPtr;
  30. #define WINDOW        (gWindowPtr)
  31. EventRecord            gEventRecord;
  32. #define    EVENT        (gEventRecord)
  33. short                gLineHeight;
  34. short                gLineAscent;
  35.  
  36. void                InitializeApplication(void);
  37. void                MakeWindow(void);
  38. void                EventLoop(void);
  39. void                DoMouseEvent(void);
  40. void                DoCommand(
  41.         long                menuChoice
  42.     );
  43. void                AdjustMenus(
  44.         WindowPtr            theWindow
  45.     );
  46. void                AdjustEditMenu(
  47.         Boolean                isDeskAcc
  48.     );
  49. /*
  50.  * Cheap 'n dirty pascal string copy routine.
  51.  */
  52. #define pstrcpy(dst, src) do {                            \
  53.         StringPtr    _src = (src);                        \
  54.         BlockMove(_src, dst, _src[0] + 1);                \
  55.     } while (0)
  56. /*
  57.  * Cheap 'n dirty pascal string concat.
  58.  */
  59. #define pstrcat(dst, src) do {                            \
  60.         StringPtr        _dst = (dst);                    \
  61.         StringPtr        _src = (src);                    \
  62.         short            _len;                            \
  63.         _len = 255 - _dst[0];                            \
  64.         if (_len > _src[0]) _len = _src[0];                \
  65.         BlockMove(&_src[1], &_dst[1] + _dst[0], _len);    \
  66.         _dst[0] += _len;                                \
  67.     } while (0)
  68.  
  69.  
  70. enum {
  71.     MENU_Apple            = 1,
  72.     MENU_File            = 128,
  73.     MENU_Edit            = 129
  74. };
  75.  
  76. enum AppleMenu {
  77.     kAppleAbout            = 1
  78. };
  79. enum FileMenu {
  80.     kFileQuit            = 1
  81. };
  82. #define kFileMenuString        \
  83.     "\pQuit/Q"
  84. enum EditMenu {
  85.     kEditUndo                = 1,
  86.     kEditUnused,
  87.     kEditCut,
  88.     kEditCopy,
  89.     kEditPaste,
  90.     kEditClear
  91. };
  92. #define kEditMenuString        \
  93.     "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear"
  94.  
  95. MenuHandle                gAppleMenu;
  96. MenuHandle                gFileMenu;
  97. MenuHandle                gEditMenu;
  98. Boolean                    gQuitNow;
  99.  
  100. LocalIdentity            gLocalIdentity;
  101. LocalIdentity            gOldLocalIdentity;
  102. Boolean                    gSystemSupportsAOCE;
  103.  
  104. enum {
  105.     kLineHasAOCE = 0,
  106.     kLineNotificationSetup,
  107.     kLineAOCEUserID,
  108.     kLastLine
  109. };
  110. Str255                    gWindowContent[kLastLine];
  111.  
  112. void                    TestSystemSupportsAOCE(void);
  113. void                    TestAuthNotificationSetup(void);
  114. void                    TestAuthNotification(void);
  115. void                    TestAuthNotificationRemoval(void);
  116. void                    TestGetUserIdentity(void);
  117. void                    ErrorMessage(
  118.         OSErr                errorStatus,
  119.         short                lineNumber,
  120.         StringPtr            message
  121.     );
  122. void                    Message(
  123.         short                lineNumber,
  124.         StringPtr            message
  125.     );        
  126. void                    DrawWindowContent(void);
  127.  
  128. OSErr                    SystemSupportsAOCE(void);
  129. OSErr                    GetUserIdentity(
  130.         AuthIdentity        *userIdentity
  131.     );
  132. OSErr                    InitAuthNotificationQueue(
  133.         LocalIdentity        *localIdentityPtr
  134.     );
  135. OSErr                    RemoveAuthNotificationQueue(void);
  136.  
  137.  
  138. void
  139. main(void)
  140. {
  141.         InitializeApplication();
  142.         MakeWindow();
  143.         TestSystemSupportsAOCE();
  144.         TestGetUserIdentity();
  145.         TestAuthNotificationSetup();
  146.         while (gQuitNow == FALSE) {
  147.             EventLoop();
  148.             TestAuthNotification();
  149.         }
  150.         TestAuthNotificationRemoval();
  151.         ExitToShell();
  152. }
  153.  
  154. void
  155. EventLoop(void)
  156. {
  157.         long                menuChoice;
  158.         WindowPtr            windowPtr;
  159.         GrafPtr                savePort;
  160.         Boolean                isActivate;
  161.         
  162.         WaitNextEvent(everyEvent, &EVENT, 10L, NULL);
  163.         switch (EVENT.what) {
  164.         case nullEvent:
  165.             break;
  166.         case keyDown:
  167.         case autoKey:
  168.             if ((EVENT.message & charCodeMask) == '.'
  169.              && (EVENT.modifiers & cmdKey) != 0) {
  170.                 FlushEvents(keyDown | autoKey, 0);
  171.                 gQuitNow = TRUE;
  172.             }
  173.             else if ((EVENT.modifiers & cmdKey) != 0) {
  174.                 if (EVENT.what == keyDown) {
  175.                     menuChoice = MenuKey(EVENT.message & charCodeMask);
  176.                     if (HiWord(menuChoice) != 0)
  177.                         DoCommand(menuChoice);
  178.                     else {
  179.                         SysBeep(10);
  180.                     }
  181.                 }
  182.             }
  183.             else {
  184.                 SysBeep(10);
  185.             }
  186.             break;
  187.         case mouseDown:
  188.             DoMouseEvent();
  189.             break;
  190.         case activateEvt:
  191.             windowPtr = (WindowPtr) EVENT.message;
  192.             isActivate = (EVENT.modifiers & activeFlag) != 0;
  193.             goto doActivate;
  194.             break;
  195.         case osEvt:
  196.             switch (((unsigned long) EVENT.message) >> 24) {
  197.             case mouseMovedMessage:
  198.                 break;
  199.             case suspendResumeMessage:
  200.                 isActivate = (EVENT.message & resumeFlag) != 0;
  201.                 windowPtr = FrontWindow();
  202. doActivate:        if (isActivate && windowPtr == WINDOW) {
  203.                     ShowWindow(windowPtr);
  204.                     SelectWindow(windowPtr);
  205.                     SetPort(windowPtr);
  206.                     InitCursor();
  207.                 }
  208.                 break;
  209.             }
  210.             break;
  211.         case updateEvt:
  212.             windowPtr = (WindowPtr) EVENT.message;
  213.             if (windowPtr == WINDOW) {
  214.                 GetPort(&savePort);
  215.                 SetPort(windowPtr);
  216.                 BeginUpdate(windowPtr);
  217.                 EraseRect(&windowPtr->portRect);
  218.                 DrawControls(windowPtr);
  219.                 DrawWindowContent();
  220.                 EndUpdate(windowPtr);
  221.                 SetPort(savePort);
  222.             }
  223.         }
  224. }
  225.  
  226. void
  227. DoMouseEvent(void)
  228. {
  229.         WindowPtr        theWindow;
  230.         short            whichPart;
  231.         
  232.         whichPart = FindWindow(EVENT.where, &theWindow);
  233.         if (whichPart == inMenuBar && theWindow != WINDOW)
  234.             theWindow = FrontWindow();
  235.         AdjustMenus(theWindow);
  236.         switch (whichPart) {
  237.         case inDesk:
  238.             SysBeep(10);
  239.             break;
  240.         case inMenuBar:
  241.             InitCursor();
  242.             DoCommand(MenuSelect(EVENT.where));
  243.             break;
  244.         case inContent:
  245.         case inGoAway:
  246.         case inDrag:
  247.             SysBeep(10);
  248.             break;
  249.         }
  250. }
  251.  
  252. void
  253. DoCommand(
  254.         long                    menuChoice
  255.     )
  256. {
  257.         short                menuItem;
  258.         Str255                menuText;
  259.         GrafPtr                savePort;
  260.         
  261.         menuItem = LoWord(menuChoice);
  262.         switch (HiWord(menuChoice)) {
  263.         case MENU_Apple:
  264.             if (menuItem != kAppleAbout) {
  265.                 GetItem(gAppleMenu, menuItem, menuText);
  266.                 AdjustEditMenu(TRUE);
  267.                 GetPort(&savePort);
  268.                 OpenDeskAcc(menuText);
  269.                 SetPort(savePort);
  270.                 AdjustEditMenu(FrontWindow() != WINDOW);
  271.             }
  272.             break;
  273.         case MENU_File:
  274.             /*
  275.              * Note that File/Measure and File/Adjust are enabled
  276.              * only if the application is in the idle state.
  277.              */
  278.             switch (menuItem) {
  279.             case kFileQuit:
  280.                 gQuitNow = TRUE;
  281.                 break;
  282.             default:
  283.                 SysBeep(10);
  284.                 break;
  285.             }
  286.             break;
  287.         case MENU_Edit:
  288.             if (SystemEdit(menuItem - 1) == FALSE)
  289.                 SysBeep(10);
  290.             break;
  291.         }
  292.         HiliteMenu(0);
  293. }        
  294.  
  295. void
  296. AdjustMenus(
  297.         WindowPtr                theWindow
  298.     )
  299. {
  300.         EnableItem(gFileMenu, kFileQuit);
  301.         AdjustEditMenu(theWindow != WINDOW);
  302. }
  303.  
  304. void
  305. AdjustEditMenu(
  306.         Boolean                isDeskAcc
  307.     )
  308. {
  309.         if (isDeskAcc) {
  310.             EnableItem(gEditMenu, kEditUndo);
  311.             EnableItem(gEditMenu, kEditCut);
  312.             EnableItem(gEditMenu, kEditCopy);
  313.             EnableItem(gEditMenu, kEditPaste);
  314.             EnableItem(gEditMenu, kEditClear);
  315.         }
  316.         else {
  317.             DisableItem(gEditMenu, kEditUndo);
  318.             DisableItem(gEditMenu, kEditCut);
  319.             DisableItem(gEditMenu, kEditCopy);
  320.             DisableItem(gEditMenu, kEditPaste);
  321.             DisableItem(gEditMenu, kEditClear);
  322.         }
  323. }
  324.  
  325. void
  326. InitializeApplication(void)
  327. {
  328.         int                i;
  329.         
  330.         InitGraf(&qd.thePort);
  331.         InitFonts();
  332.         InitWindows();
  333.         InitMenus();
  334.         TEInit();
  335.         InitDialogs(NULL);
  336.         SetCursor(*GetCursor(watchCursor));
  337.         for (i = 0; i < 3; i++)
  338.             EventAvail(everyEvent, &EVENT);
  339.         gAppleMenu = NewMenu(MENU_Apple, "\p\024");
  340.         AppendMenu(gAppleMenu, "\p(© 1992-93 Apple Computer Inc.;(-");
  341.         AddResMenu(gAppleMenu, 'DRVR');
  342.         InsertMenu(gAppleMenu, 0);
  343.         gFileMenu = NewMenu(MENU_File, "\pFile");
  344.         AppendMenu(gFileMenu, kFileMenuString);
  345.         InsertMenu(gFileMenu, 0);
  346.         gEditMenu = NewMenu(MENU_Edit, "\pEdit");
  347.         AppendMenu(gEditMenu, kEditMenuString);
  348.         InsertMenu(gEditMenu, 0);
  349.         DrawMenuBar();
  350.         InitCursor();
  351. }
  352.  
  353. void
  354. MakeWindow(void)
  355. {
  356.         Rect            boundsRect;
  357.         FontInfo        info;
  358.         
  359.         SetRect(
  360.             &boundsRect,
  361.             40, 40, 400, 300
  362.         );
  363.         WINDOW = NewWindow(
  364.                 NULL,                    /* Allocate window    */
  365.                 &boundsRect,
  366.                 "\pSnippit Test",
  367.                 TRUE,
  368.                 plainDBox,
  369.                 (WindowPtr) -1L,
  370.                 FALSE,                    /* No go away box    */
  371.                 0
  372.             );
  373.         if (WINDOW == NULL) {
  374.             SysBeep(10);
  375.             SysBeep(10);
  376.             ExitToShell();
  377.         }
  378.         SetPort(WINDOW);
  379.         TextFont(geneva);
  380.         TextSize(9);
  381.         GetFontInfo(&info);
  382.         gLineHeight = info.ascent + info.descent + info.leading;
  383.         gLineAscent = info.ascent;
  384. }
  385.  
  386. /*
  387.  * Snippit test routines.
  388.  */
  389. void
  390. TestSystemSupportsAOCE(void)
  391. {
  392.         OSErr                status;
  393.         
  394.         status = SystemSupportsAOCE();
  395.         switch (status) {
  396.         case noErr:
  397.             Message(kLineHasAOCE, "\pAOCE is enabled");
  398.             gSystemSupportsAOCE = TRUE;
  399.             break;
  400.         case kOCEToolboxNotOpen:
  401.             Message(kLineHasAOCE, "\pAOCE is present, but disabled");
  402.             gSystemSupportsAOCE = FALSE;
  403.             break;
  404.         case gestaltUndefSelectorErr:
  405.             Message(kLineHasAOCE, "\pAOCE is not present on this machine");
  406.             gSystemSupportsAOCE = FALSE;
  407.             break;
  408.         default:
  409.             ErrorMessage(status, kLineHasAOCE, "\pTesting for AOCE presence");
  410.             gSystemSupportsAOCE = FALSE;
  411.             break;
  412.         }
  413. }
  414.  
  415. void
  416. TestGetUserIdentity(void)
  417. {
  418.         OSErr                    status;
  419.         
  420.         if (gSystemSupportsAOCE == FALSE)
  421.             Message(kLineAOCEUserID, "\pCan't test user identity");
  422.         else {
  423.             status = GetUserIdentity(&gLocalIdentity);
  424.             switch (status) {
  425.             case noErr:
  426.                 Message(kLineAOCEUserID, "\pGot user identity");
  427.                 break;
  428.             case userCanceledErr:
  429.                 Message(kLineAOCEUserID, "\pUser canceled Get Identity");
  430.                 break;
  431.             default:
  432.                 ErrorMessage(status, kLineAOCEUserID, "\pGet identity failed");
  433.                 break;
  434.             }
  435.         }
  436. }
  437.  
  438. void
  439. TestAuthNotificationSetup(void)
  440. {
  441.         OSErr                    status;
  442.         
  443.         if (gSystemSupportsAOCE == FALSE) {
  444.             Message(
  445.                 kLineNotificationSetup,
  446.                 "\pCan't test identity notification queue"
  447.             );
  448.         }
  449.         else {
  450.             /*
  451.              * Make sure that TestAuthNotification sees the change.
  452.              */
  453.             gOldLocalIdentity = (~gLocalIdentity);
  454.             status = InitAuthNotificationQueue(&gLocalIdentity);
  455.             switch (status) {
  456.             case noErr:
  457.                 Message(
  458.                     kLineNotificationSetup,
  459.                     "\pIdentity notification queue initialized"
  460.                 );
  461.                 break;
  462.             default:
  463.                 ErrorMessage(
  464.                     status,
  465.                     kLineNotificationSetup,
  466.                     "\pIdentity notification queue setup failed"
  467.                 );
  468.                 break;
  469.             }
  470.         }
  471. }
  472.  
  473.  
  474. void
  475. TestAuthNotification(void)
  476. {
  477.         Str255                    msg;
  478.         
  479.         if (gOldLocalIdentity != gLocalIdentity) {
  480.             gOldLocalIdentity = gLocalIdentity;
  481.             if (gOldLocalIdentity == 0)
  482.                 Message(kLineAOCEUserID, "\pAOCE Locked");
  483.             else {
  484.                 NumToString(gLocalIdentity, msg);
  485.                 pstrcat(msg, "\p: current local identity");
  486.                 Message(kLineAOCEUserID, msg);
  487.             }
  488.         }
  489. }
  490.  
  491. /*
  492.  * Since this is called when the program exits, we can't
  493.  * really test for error returns. Thus, we trap into DebugStr
  494.  * on errors.
  495.  */
  496. void
  497. TestAuthNotificationRemoval(void)
  498. {
  499.         OSErr                status;
  500.         
  501.         if (gSystemSupportsAOCE) {
  502.             status = RemoveAuthNotificationQueue();
  503.             if (status != noErr)
  504.                 DebugStr("\pRemoveAuthNotifyQueue error");
  505.         }
  506. }
  507.  
  508.  
  509. void
  510. ErrorMessage(
  511.         OSErr                errorStatus,
  512.         short                lineNumber,
  513.         StringPtr            message
  514.     )
  515. {
  516.         Str15                errorNumber;
  517.         Str255                msg;
  518.         
  519.         NumToString(errorStatus, errorNumber);
  520.         pstrcpy(msg, "\pSystem error: ");
  521.         pstrcat(msg, errorNumber);
  522.         pstrcat(msg, message);
  523.         Message(lineNumber, message);
  524. }
  525.  
  526. void
  527. Message(
  528.         short                lineNumber,
  529.         StringPtr            message
  530.     )
  531. {
  532.         Rect                boundsRect;
  533.         
  534.         SetPort(WINDOW);
  535.         pstrcpy(gWindowContent[lineNumber], message);
  536.         SetRect(
  537.             &boundsRect,
  538.             WINDOW->portRect.left,
  539.             WINDOW->portRect.top + (lineNumber * gLineHeight) + 4,
  540.             WINDOW->portRect.right,
  541.             0
  542.         );
  543.         boundsRect.bottom = boundsRect.top + gLineHeight;
  544.         InvalRect(&boundsRect);
  545. }
  546.  
  547. void
  548. DrawWindowContent(void)
  549. {
  550.         short                i;
  551.         
  552.         for (i = 0; i < kLastLine; i++) {
  553.             MoveTo(
  554.                 4,
  555.                 WINDOW->portRect.top
  556.                     + (i * gLineHeight)
  557.                     + 4
  558.                     + gLineAscent
  559.             );
  560.             DrawString(gWindowContent[i]);
  561.         }
  562. }
  563.  
  564.  
  565.