procedure TProgram.Run; begin Execute; end; function TGroup.Execute: Word; var E: TEvent; begin repeat EndState := 0; repeat GetEvent(E); HandleEvent(E); if E.What <> evNothing then EventError(E); until EndState <> 0; until Valid(EndState); Execute := EndState; end; type { Event record } PEvent = ^TEvent; TEvent = record What: Word; case Word of evNothing: (); evMouse: ( Buttons: Byte; Double: Boolean; Where: TPoint); evKeyDown: ( case Integer of 0: (KeyCode: Word); 1: (CharCode: Char; ScanCode: Byte)); evMessage: ( Command: Word; case Word of 0: (InfoPtr: Pointer); 1: (InfoLong: Longint); 2: (InfoWord: Word); 3: (InfoInt: Integer); 4: (InfoByte: Byte); 5: (InfoChar: Char)); end; procedure TProgram.GetEvent(var Event: TEvent); var R: TRect; function ContainsMouse(P: PView): Boolean; far; begin ContainsMouse := (P^.State and sfVisible <> 0) and P^.MouseInView(Event.Where); end; begin if Pending.What <> evNothing then begin Event := Pending; Pending.What := evNothing; end else begin GetMouseEvent(Event); if Event.What = evNothing then begin GetKeyEvent(Event); if Event.What = evNothing then Idle; end; end; if StatusLine <> nil then if (Event.What and evKeyDown <> 0) or (Event.What and evMouseDown <> 0) and (FirstThat(@ContainsMouse) = PView(StatusLine)) then StatusLine^.HandleEvent(Event); end; procedure TProgram.HandleEvent(var Event: TEvent); var I: Word; C: Char; begin if Event.What = evKeyDown then begin C := GetAltChar(Event.KeyCode); if (C >= '1') and (C <= '9') then if Message(Desktop, evBroadCast, cmSelectWindowNum, Pointer(Byte(C) - $30)) <> nil then ClearEvent(Event); end; TGroup.HandleEvent(Event); if Event.What = evCommand then if Event.Command = cmQuit then begin EndModal(cmQuit); ClearEvent(Event); end; end; procedure TProgram.Idle; begin if StatusLine <> nil then StatusLine^.Update; if CommandSetChanged then begin Message(@Self, evBroadcast, cmCommandSetChanged, nil); CommandSetChanged := False; end; end procedure TTVDemo.GetEvent(var Event: TEvent); var W: PWindow; HFile: PHelpFile; HelpStrm: PDosStream; const HelpInUse: Boolean = False; begin inherited GetEvent(Event); case Event.What of evCommand: if (Event.Command = cmHelp) and not HelpInUse then begin HelpInUse := True; HelpStrm := New(PDosStream, Init(CalcHelpName, stOpenRead)); HFile := New(PHelpFile, Init(HelpStrm)); if HelpStrm^.Status <> stOk then begin MessageBox('Could not open help file.', nil, mfError + mfOkButton); Dispose(HFile, Done); end else begin W := New(PHelpWindow,Init(HFile, GetHelpCtx)); if ValidView(W) <> nil then begin ExecView(W); Dispose(W, Done); end; ClearEvent(Event); end; HelpInUse := False; end; evMouseDown: if Event.Buttons <> 1 then Event.What := evNothing; end; end; procedure TTVDemo.HandleEvent(var Event: TEvent); begin inherited HandleEvent(Event); case Event.What of evCommand: begin case Event.Command of cmOpen: FileOpen('*.*'); cmNew: FileNew; cmShowClip: ShowClip; cmChangeDir: ChangeDir; cmAbout: About; cmPuzzle: Puzzle; cmCalendar: Calendar; cmAsciiTab: AsciiTab; cmCalculator: Calculator; cmColors: Colors; cmMouse: Mouse; cmSaveDesktop: SaveDesktop; cmRetrieveDesktop: RetrieveDesktop; else Exit; end; ClearEvent(Event); end; end; end procedure TApplication.HandleEvent(var Event: TEvent); begin inherited HandleEvent(Event); case Event.What of evCommand: begin case Event.Command of cmTile: Tile; cmCascade: Cascade; cmDosShell: DosShell; else Exit; end; ClearEvent(Event); end; end; end; procedure TProgram.HandleEvent(var Event: TEvent); var I: Word; C: Char; begin if Event.What = evKeyDown then begin C := GetAltChar(Event.KeyCode); if (C >= '1') and (C <= '9') then if Message(Desktop, evBroadCast, cmSelectWindowNum, Pointer(Byte(C) - $30)) <> nil then ClearEvent(Event); end; TGroup.HandleEvent(Event); if Event.What = evCommand then if Event.Command = cmQuit then begin EndModal(cmQuit); ClearEvent(Event); end; end; procedure TGroup.HandleEvent(var Event: TEvent); procedure DoHandleEvent(P: PView); far; begin if (P = nil) or ((P^.State and sfDisabled <> 0) and (Event.What and (PositionalEvents or FocusedEvents) <> 0)) then Exit; case Phase of phPreProcess: if P^.Options and ofPreProcess = 0 then Exit; phPostProcess: if P^.Options and ofPostProcess = 0 then Exit; end; if Event.What and P^.EventMask <> 0 then P^.HandleEvent(Event); end; function ContainsMouse(P: PView): Boolean; far; begin ContainsMouse := (P^.State and sfVisible <> 0) and P^.MouseInView(Event.Where); end; begin TView.HandleEvent(Event); if Event.What and FocusedEvents <> 0 then begin Phase := phPreProcess; ForEach(@DoHandleEvent); Phase := phFocused; DoHandleEvent(Current); Phase := phPostProcess; ForEach(@DoHandleEvent); end else begin Phase := phFocused; if (Event.What and PositionalEvents <> 0) then DoHandleEvent(FirstThat(@ContainsMouse)) else ForEach(@DoHandleEvent); end; end; function Message(Receiver: PView; What, Command: Word; InfoPtr: Pointer): Pointer; var Event: TEvent; begin Message := nil; if Receiver <> nil then begin Event.What := What; Event.Command := Command; Event.InfoPtr := InfoPtr; Receiver^.HandleEvent(Event); if Event.What = evNothing then Message := Event.InfoPtr; end; end; procedure TView.GetEvent(var Event: TEvent); begin if Owner <> nil then Owner^.GetEvent(Event); end;