\ Downloaded from the Forth SIG on Compuserve \ MSFLTR.MFP 04-Feb-88 5120 8 { Keywords: EVENTS EVENT FILTER MOUSE ACTIVE WINDOW Shows one way of handling mouse events in an active window without activating it. New version allowing movement of window. } \ In response to a number of inquiries on the net----- \ This file demostrates a way of handling mouse events in a window \ without activating the window by use of an event filter. Also silencing \ of the beep when mouse goes down in desktop is demonstrated. \ The test window may be moved without activating it, using the standard \ Command-Key-Mouse down-in-drag-box combination. \ Sample code only...verify before using a variation in Production code! \ Structures and certain other phraseology are borrowed from John Baxter's \ CMDTST.MFP, a filter demo which deals with key strokes. \ Ben Goodwin 76327,13 anew FilterTest structure TEventMessage \ NOTE: only the form of immediate interest! short: emFiller byte: emKeyCode byte: emCharacter structure.end structure TEventRecord \ An event record short: erEventCode TEventMessage struct: erMessage long: erTicks point: erMouseLoc short: erModifiers structure.end NEW.WINDOW TEST.WINDOW 0 0 100 200 TEST.WINDOW W.BOUNDS SYS.WINDOW TEST.WINDOW W.BEHIND NOT.VISIBLE TEST.WINDOW W.ATTRIBUTES TEST.WINDOW CENTER.WINDOW TEST.WINDOW ADD.WINDOW \ Create a rectangle extending bounds rect to full area of window TEST.WINDOW +WBOUNDS @RECT RECT OUTLINE.RECT OUTLINE.RECT W@ 20 - OUTLINE.RECT W! CREATE HEIGHT OUTLINE.RECT 4+ W@ OUTLINE.RECT W@ - , CREATE WIDTH OUTLINE.RECT 6+ W@ OUTLINE.RECT 2+ W@ - , 10 10 50 50 RECT MY.RECT 0 0 0 0 RECT GLOBAL.RECT CREATE INSTALLED 0 , CREATE REMOVED 0 , CREATE TEST.VAR 0 , CREATE COMMAND.KEY.MOVE 0 , hex 100 constant kCommandKey decimal \ Command key mask for Modifiers \ Convert rectangle to global coordinates to function with mouse location \ from event record. : DRAW.RECT ( --- ) TEST.WINDOW WINDOW PAGE GINIT MY.RECT @RECT XY>GLOBAL ( --- ) TEST.WINDOW WINDOW MY.RECT @ LOCAL>GLOBAL GLOBAL.RECT ! TEST.WINDOW WINDOW MY.RECT 4+ @ LOCAL>GLOBAL GLOBAL.RECT 4+ ! SYS.WINDOW WINDOW ; : MY.RECT>LOCAL ( --- ) TEST.WINDOW WINDOW MY.RECT @ GLOBAL>LOCAL GLOBAL.RECT ! TEST.WINDOW WINDOW MY.RECT 4+ @ GLOBAL>LOCAL GLOBAL.RECT 4+ ! SYS.WINDOW WINDOW ; : ON.CLICK ( --- ) TEST.WINDOW WINDOW 0 TEXTFONT 10 70 MOVE.TO ." Now Click various spots" 10 85 MOVE.TO ." on Screen" 4 textfont SYS.WINDOW WINDOW ; : UPDATE.OUTLINE ( --- ) TEST.WINDOW WINDOW 0 0 XY>POINT LOCAL>GLOBAL POINT>XY 20 - XY>POINT OUTLINE.RECT ! OUTLINE.RECT W@ HEIGHT @ + OUTLINE.RECT 4+ W! OUTLINE.RECT 2+ W@ WIDTH @ + OUTLINE.RECT 6+ W! SYS.WINDOW WINDOW ; : PROMPTS ( --- ) sys.window window page ginit 0 textfont cr ." Close all windows except the test window (if open) and MacForth Window. " cr ." Type Install to install filter. " cr ." Type Remove to deactivate filter. " cr ." Click MacForth window and type Install to reinstall filter. " 4 textfont ; : MyFilter ( code -- code ) locals| theCode | COMMAND.KEY.MOVE @ \ non-zero if window has been moved IF UPDATE.OUTLINE MY.RECT>GLOBAL COMMAND.KEY.MOVE OFF THEN theCode IN.LOWER.WINDOW = theCode IN.DESKTOP = ( to silence beep in desktop ) OR IF 0 TO theCode \ CR EVENT.RECORD erMouseLoc @ POINT>XY SWAP . . EVENT.RECORD erMouseLoc @ GLOBAL.RECT PTINRECT IF 3 0 DO 1 SYSBEEP LOOP TEST.WINDOW WINDOW ON.CLICK THEN THEN \ Prevent activation of window when mouse down in drag box without \ disabling ability to drag active window theCode IN.DRAG.BOX = IF EVENT.RECORD erMouseLoc @ OUTLINE.RECT PTINRECT IF EVENT.RECORD ERMODIFIERS W@ KCOMMANDKEY AND NOT \ mask to check \ whether command key down IF 0 TO theCode \ nullify default handling of event \ CR EVENT.RECORD erMouseLoc @ POINT>XY SWAP . . ELSE COMMAND.KEY.MOVE ON \ indicates movement of window THEN THEN THEN theCode ; \ pass along code : Install ( --- | Set up the event filter...assume prior contents are 0 ) INSTALLED @ NOT IF \ Semaphore to prevent duplicate Local>Global TEST.WINDOW SHOW.WINDOW prompts " Click Rectangle" TEST.WINDOW SET.WTITLE DRAW.RECT token.for myfilter event.filter ! MY.RECT>GLOBAL UPDATE.OUTLINE INSTALLED ON REMOVED OFF THEN ; : ON.REMOVAL ( --- ) 0 textfont ." Click this window" 4 textfont ; : Remove ( --- | Clear the event filter...to the assumed 0 initial cond.) REMOVED @ NOT IF \ Semaphore to prevent duplicate Local>Global TEST.WINDOW WINDOW PAGE ON.REMOVAL " Filter Removed" TEST.WINDOW SET.WTITLE event.filter off MY.RECT>LOCAL SYS.WINDOW WINDOW INSTALLED OFF REMOVED ON THEN ; : TEST.ACT IF PAGE ." Active Window" ELSE PAGE THEN ; TEST.WINDOW ON.UPDATE DO.RECT TEST.WINDOW ON.ACTIVATE TEST.ACT SYS.WINDOW SELECT.WINDOW PROMPTS