home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Libraries / WASTE 1.1a4 / Demo Source / WEDemoEvents.p < prev    next >
Encoding:
Text File  |  1994-11-10  |  10.3 KB  |  447 lines  |  [TEXT/PJMM]

  1. unit DemoEvents;
  2.  
  3. { WASTE DEMO PROJECT: }
  4. { Events Handling }
  5.  
  6. { Copyright © 1993-1994 Merzwaren }
  7. { All Rights Reserved }
  8.  
  9. interface
  10.     uses
  11.         DemoIntf;
  12.  
  13.     function InitializeEvents: OSErr;
  14.     procedure DoWindowEvent (var event: EventRecord);
  15.     procedure ProcessEvent;
  16.  
  17. implementation
  18.     uses
  19.         AppleEvents, AERegistry, DemoMenus, DemoScripting, DemoWindows, Segments, TextServices;
  20.  
  21.     const
  22.  
  23. { possible values for HandleOpenDocument refCon parameter }
  24.  
  25.         kDoOpen = 0;
  26.         kDoPrint = 1;
  27.  
  28.     var
  29.  
  30.         gSleepTime: LongInt;                    { sleep time for WaitNextEvent }
  31.         gMouseRgn: RgnHandle;                { mouse region for WaitNextEvent }
  32.  
  33.     procedure AdjustCursor (mouseLoc: Point;
  34.                                     mouseRgn: RgnHandle);
  35.         var
  36.             window: WindowPtr;
  37.     begin
  38.  
  39. { by default, set mouseRgn to the whole QuickDraw coordinate plane, }
  40. { so that we never get mouse moved events }
  41.         SetRectRgn(mouseRgn, -maxint, -maxint, maxint, maxint);
  42.  
  43. { give text services a chance to set the cursor shape }
  44.         if (gHasTextServices) then
  45.             if (SetTSMCursor(mouseLoc)) then
  46.                 Exit(AdjustCursor);
  47.  
  48. { if there is a window open, give WEAdjustCursor an opportunity to set the cursor }
  49. { WEAdjustCursor intersects mouseRgn (if supplied) with a region within which }
  50. { the cursor is to retain its shape. }
  51. { (if the cursor is outside the view region, this is subtracted from mouseRgn) }
  52.  
  53.         window := FrontWindow;
  54.         if (window <> nil) then
  55.             if (WEAdjustCursor(mouseLoc, mouseRgn, DocumentPeek(window)^.hWE)) then
  56.                 Exit(AdjustCursor);
  57.  
  58. { set the cursor to the arrow cursor }
  59.         SetCursor(arrow);
  60.  
  61.     end;  { AdjustCursor }
  62.  
  63.     procedure DoMouseDown (var event: EventRecord);
  64.         var
  65.             window: WindowPtr;
  66.             partCode: Integer;
  67.     begin
  68.  
  69. { find out where this click went down in }
  70.         partCode := FindWindow(event.where, window);
  71.  
  72. { dispatch on partCode }
  73.         case partCode of
  74.  
  75.             inMenuBar: 
  76.                 begin
  77.                     PrepareMenus;
  78.                     DoMenuChoice(MenuSelect(event.where));
  79.                 end;
  80.  
  81.             inSysWindow: 
  82.                 SystemClick(event, window);
  83.  
  84.             inContent: 
  85.                 DoContent(event.where, event, window);
  86.  
  87.             inDrag: 
  88.                 DoDrag(event.where, window);
  89.  
  90.             inGrow: 
  91.                 DoGrow(event.where, window);
  92.  
  93.             inGoAway: 
  94.                 if (TrackGoAway(window, event.where)) then
  95.                     if (DoClose(closingWindow, savingAsk, window) <> noErr) then
  96.                         ;
  97.  
  98.             inZoomIn, inZoomOut: 
  99.                 if (TrackBox(window, event.where, partCode)) then
  100.                     DoZoom(partCode, window);
  101.  
  102.             otherwise
  103.                 ;
  104.         end;  { case partCode }
  105.     end;  { DoMouseDown }
  106.  
  107.     procedure DoKeyDown (var event: EventRecord);
  108.         const
  109.  
  110. { virtual key codes generated by some function keys }
  111.             keyF1 = $7A;
  112.             keyF2 = $78;
  113.             keyF3 = $63;
  114.             keyF4 = $76;
  115.         var
  116.             key: Char;
  117.     begin
  118.  
  119. { extract character code from event message }
  120.         key := Char(BitAnd(event.message, charCodeMask));
  121.  
  122. { map function keys to the equivalent command+key combos }
  123. { note that all function keys generate the same character code, i.e. $10 }
  124.         if (key = CHR($10)) then
  125.             begin
  126.                 event.modifiers := BitOr(event.modifiers, cmdKey);
  127.                 case BSR(BitAnd(event.message, keyCodeMask), 8) of
  128.  
  129.                     keyF1: 
  130.                         key := 'z';
  131.  
  132.                     keyF2: 
  133.                         key := 'x';
  134.  
  135.                     keyF3: 
  136.                         key := 'c';
  137.  
  138.                     keyF4: 
  139.                         key := 'v';
  140.  
  141.                     otherwise
  142.                         key := CHR(0);
  143.                 end;  { case }
  144.             end;
  145.  
  146. { command + printable character combos are routed to MenuKey }
  147.         if (BitAnd(event.modifiers, cmdKey) <> 0) and (key >= CHR(32)) then
  148.             begin
  149.                 PrepareMenus;
  150.                 DoMenuChoice(MenuKey(key));
  151.             end
  152.         else
  153.             DoKey(key, event);
  154.     end;  { DoKeyDown }
  155.  
  156.     procedure DoDiskEvent (var event: EventRecord);
  157.         var
  158.             dialogCorner: Point;
  159.             err: OSErr;
  160.     begin
  161.         if (BSR(event.message, 16) <> noErr) then
  162.             begin
  163.                 SetPt(dialogCorner, 112, 80);
  164.                 err := DIBadMount(dialogCorner, event.message);
  165.             end;
  166.     end;  { DoDiskEvent }
  167.  
  168.     procedure DoOSEvent (var event: EventRecord);
  169.         var
  170.             osMessage: Integer;
  171.             window: WindowPtr;
  172.     begin
  173.  
  174. { extract the OS message field from the event record }
  175.         osMessage := BSR(BAND(event.message, osEvtMessageMask), 24);
  176.  
  177. { dispatch on osMessage }
  178.         case osMessage of
  179.  
  180.             suspendResumeMessage: 
  181.                 begin
  182.                     window := FrontWindow;
  183.                     if (window <> nil) then
  184.                         DoActivate(BitAnd(event.message, resumeFlag) <> 0, window);
  185.                 end;
  186.  
  187.             mouseMovedMessage: 
  188.                 ;
  189.  
  190.             otherwise
  191.                 ;
  192.         end;  { case }
  193.     end;  { DoOSEvent }
  194.  
  195.     procedure DoHighLevelEvent (var event: EventRecord);
  196.         var
  197.             err: OSErr;
  198.     begin
  199.         err := AEProcessAppleEvent(event);
  200.     end;  { DoHighLevelEvent }
  201.  
  202.     procedure DoNullEvent (var event: EventRecord);
  203.         var
  204.             window: WindowPtr;
  205.     begin
  206.         window := FrontWindow;
  207.         if (window <> nil) then
  208.             WEIdle(gSleepTime, DocumentPeek(window)^.hWE)
  209.         else
  210.             gSleepTime := maxLongInt;
  211.  
  212. { unload (= unlock & mark as purgeable) non-persistent code segments }
  213.         UnloadNonPersistentSegments;
  214.  
  215.     end;  { DoNullEvent }
  216.  
  217.     procedure DoWindowEvent (var event: EventRecord);
  218.         var
  219.             window: WindowPtr;
  220.     begin
  221.  
  222. { the message field of the event record contains the window pointer }
  223.         window := WindowPtr(event.message);
  224.  
  225. { make sure this window is an application window; check the windowKind field }
  226.         if (WindowPeek(window)^.windowKind <> userKind) then
  227.             Exit(DoWindowEvent);
  228.  
  229.         case event.what of
  230.  
  231.             updateEvt: 
  232.                 DoUpdate(window);
  233.  
  234.             activateEvt: 
  235.                 DoActivate(BitAnd(event.modifiers, activeFlag) <> 0, window);
  236.  
  237.         end;  { case }
  238.     end;  { DoWindowEvent }
  239.  
  240.     procedure ProcessEvent;
  241.         var
  242.             event: EventRecord;
  243.             gotEvent: Boolean;
  244.     begin
  245.  
  246.         gotEvent := WaitNextEvent(everyEvent, event, gSleepTime, gMouseRgn);
  247.  
  248. { give text services a chance to intercept this event }
  249.         if (gHasTextServices) then
  250.             if TSMEvent(event) then
  251.                 ;
  252.  
  253. { adjust cursor shape and set mouse region }
  254. { (we assume event.where is the current mouse position in global coordinates }
  255. { if event.what <= osEvt; high-level events store the event ID there) }
  256.  
  257.         if (event.what <= osEvt) then
  258.             AdjustCursor(event.where, gMouseRgn);
  259.  
  260. { dispatch on event.what }
  261.         case event.what of
  262.  
  263.             nullEvent: 
  264.                 DoNullEvent(event);
  265.  
  266.             mouseDown: 
  267.                 DoMouseDown(event);
  268.  
  269.             keyDown, autoKey: 
  270.                 DoKeyDown(event);
  271.  
  272.             updateEvt, activateEvt: 
  273.                 DoWindowEvent(event);
  274.  
  275.             diskEvt: 
  276.                 DoDiskEvent(event);
  277.  
  278.             osEvt: 
  279.                 DoOSEvent(event);
  280.  
  281.             kHighLevelEvent: 
  282.                 DoHighLevelEvent(event);
  283.  
  284.             otherwise
  285.                 ;                                            { ignore other events }
  286.         end;  { case }
  287.  
  288.         if (gotEvent) then
  289.             gSleepTime := 0;                        { force early idle after non-idle event }
  290.  
  291.     end;  { ProcessEvent }
  292.  
  293.     function GotRequiredParams (ae: AppleEvent): OSErr;
  294.         var
  295.             returnedType: DescType;
  296.             actualSize: Size;
  297.             err: OSErr;
  298.     begin
  299.         err := AEGetAttributePtr(ae, keyMissedKeywordAttr, typeWildCard, returnedType, nil, 0, actualSize);
  300.         if (err = errAEDescNotFound) then
  301.             GotRequiredParams := noErr
  302.         else if (err = noErr) then
  303.             GotRequiredParams := errAEParamMissed
  304.         else
  305.             GotRequiredParams := err;
  306.     end;  { GotRequiredParams }
  307.  
  308.     function HandleOpenDocument (ae, reply: AppleEvent;
  309.                                     refCon: LongInt): OSErr;
  310.         var
  311.             docList: AEDescList;
  312.             keyword: AEKeyword;
  313.             returnedType: DescType;
  314.             actualSize: Size;
  315.             numberOfDocuments, i: LongInt;
  316.             fileSpec: FSSpec;
  317.  
  318.         procedure CheckErr (err: OSErr);
  319.         begin
  320.             if (err <> noErr) then
  321.                 begin
  322.                     HandleOpenDocument := err;
  323.                     err := AEDisposeDesc(docList);
  324.                     Exit(HandleOpenDocument);
  325.                 end;
  326.         end;  { CheckErr }
  327.  
  328.     begin
  329.         HandleOpenDocument := noErr;
  330.  
  331. { extract direct parameter from the Apple Event }
  332.         CheckErr(AEGetParamDesc(ae, keyDirectObject, typeAEList, docList));
  333.  
  334. { perform the recommended check for additional required parameters }
  335.         CheckErr(GotRequiredParams(ae));
  336.  
  337. { count the items in the list of aliases }
  338.         CheckErr(AECountItems(docList, numberOfDocuments));
  339.  
  340.         for i := 1 to numberOfDocuments do
  341.             begin
  342.  
  343. { coerce the nth alias to a file system specification record }
  344.                 CheckErr(AEGetNthPtr(docList, i, typeFSS, keyword, returnedType, @fileSpec, SizeOf(fileSpec), actualSize));
  345.  
  346. { open the specified file }
  347.                 CheckErr(CreateWindow(@fileSpec));
  348.             end;  { for }
  349.  
  350. { dispose of the alias list }
  351.         CheckErr(AEDisposeDesc(docList));
  352.  
  353.     end;  { HandleOpenDocument }
  354.  
  355.     function HandleOpenApplication (ae, reply: AppleEvent;
  356.                                     refCon: LongInt): OSErr;
  357.         var
  358.             err: OSErr;
  359.     begin
  360.  
  361. { perform the recommended check for additional required parameters }
  362.         err := GotRequiredParams(ae);
  363.         if (err <> noErr) then
  364.             begin
  365.                 HandleOpenApplication := err;
  366.                 Exit(HandleOpenApplication);
  367.             end;
  368.  
  369. { create a new window from scratch }
  370.         HandleOpenApplication := CreateWindow(nil);
  371.  
  372.     end;  { HandleOpenApplication }
  373.  
  374.     function HandleQuitApplication (ae, reply: AppleEvent;
  375.                                     refCon: LongInt): OSErr;
  376.         var
  377.             optKey: AEKeyword;
  378.             actualType: DescType;
  379.             actualSize: Size;
  380.             saving: SavingOption;
  381.             err: OSErr;
  382.     begin
  383.  
  384. { default saving option is savingAsk }
  385.         saving := savingAsk;
  386.  
  387. { extract the optional save options }
  388.         err := AEGetKeyPtr(ae, keyAESaveOptions, typeEnumerated, actualType, @optKey, SizeOf(optKey), actualSize);
  389.         if (err = noErr) then
  390.             begin
  391.                 if (optKey = kAEYes) then
  392.                     saving := savingYes
  393.                 else if (optKey = kAENo) then
  394.                     saving := savingNo
  395.                 else if (optKey <> kAEAsk) then
  396.                     begin
  397.                         HandleQuitApplication := paramErr;        { for want of a better code }
  398.                         Exit(HandleQuitApplication);
  399.                     end;
  400.             end;
  401.  
  402. { perform the recommended check for additional required parameters }
  403.         err := GotRequiredParams(ae);
  404.         if (err <> noErr) then
  405.             begin
  406.                 HandleQuitApplication := err;
  407.                 Exit(HandleQuitApplication);
  408.             end;
  409.  
  410.         HandleQuitApplication := DoQuit(saving);
  411.     end;  { HandleQuitApplication }
  412.  
  413. { THINK Pascal compiler directive: put the following code in the "Init" segment }
  414. {$S Init}
  415.  
  416.     function InitializeEvents: OSErr;
  417.  
  418.         procedure CheckErr (err: OSErr);
  419.         begin
  420.             if (err <> noErr) then
  421.                 begin
  422.                     InitializeEvents := err;
  423.                     Exit(InitializeEvents);
  424.                 end;
  425.         end;  { CheckErr }
  426.  
  427.     begin
  428.         InitializeEvents := noErr;
  429.  
  430. { allocate space for the mouse region }
  431.         gMouseRgn := NewRgn;
  432.  
  433. { install Apple event handlers for the Required Suite }
  434.         CheckErr(AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOpenApplication, 0, false));
  435.         CheckErr(AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleOpenDocument, kDoOpen, false));
  436.         CheckErr(AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandleOpenDocument, kDoPrint, false));
  437.         CheckErr(AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQuitApplication, 0, false));
  438.  
  439. { install Apple event handlers for a subset of the Core Suite }
  440.         CheckErr(InstallCoreHandlers);
  441.  
  442. { install Apple event handlers for inline input }
  443.         CheckErr(WEInstallTSMHandlers);
  444.  
  445.     end;  { InitializeEvents }
  446.  
  447. end.