home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / database / pk4pak.zip / TRACER.SC < prev    next >
Text File  |  1993-01-06  |  14KB  |  422 lines

  1. ;============================================================================
  2. ; (c) Copyright Elect Software International Inc., 1992, Toronto. Anyone can
  3. ; use this code for anything as long as it is not resold as a software
  4. ; development resource, as long as the copyright notice isn't removed, as
  5. ; long as changes are clearly marked as to authorship, and as long as users
  6. ; indemnify Elect from any liability.
  7. ; Comments welcome. Henrik Bechmann, CIS:72701,3717; Tel:416-534-8176.
  8. ;============================================================================
  9.  
  10. ; Tracer Version 1.03 October 31, 1992.
  11. ;
  12. ;============================================================================
  13. ;                              TRACER DESCRIPTION
  14. ;============================================================================
  15. ;
  16. ; Tracer is an add-in for EventMan (found in EVENTMAN.SC). It traces events
  17. ; (EventMan.EventTag) generated by EventMan. It works by in setting
  18. ; EventMan.DispatchEventTagProc to "Tracer.TraceEvent", writing the current
  19. ; EventMan.EventTag to the tracer window and/or file, and then EXECPROCing
  20. ; the original EventMan.DispatchEventTagProc.
  21. ;
  22. ; Load Tracer by simply issuing Tracer.Constructor() after your application
  23. ; has set EventMan.DispatchEventTagProc to its own event dispatcher (or has
  24. ; left the default value). EventMan.Constuctor() sets a default handler of
  25. ; "-20" for [AltT] in EventMan.Dictionary[] to call the Tracer Menu, so if
  26. ; you over-ride EventMan.DispatchEventTagProc with your own, your dispatcher
  27. ; should be written to recognize such default events.
  28. ;
  29. ; Unload Tracer with Tracer.Destructor()
  30. ;
  31. ; See EDEMO.SC for an example of the use of Tracer.
  32. ;
  33. ;============================================================================
  34. ;                              TRACER INTERFACE
  35. ;============================================================================
  36.  
  37. ; Tracer.Constructor()
  38. ; Tracer.Destructor()
  39. ;
  40. ; Tracer.OpenEventTrace()
  41. ; Tracer.CloseEventTrace()
  42. ; Tracer.OpenEventFile()
  43. ; Tracer.CloseEventFile()
  44. ; Tracer.SaveAndCloseFile()
  45. ;
  46. ; Tracer.ControlMenu()
  47. ; Tracer.TraceControl()
  48. ; Tracer.TraceEvent()
  49. ; Tracer.TraceEventToWindow(ObjectTag,EventTag)
  50. ;
  51. ; Tracer.TraceWindowHandle
  52. ; Tracer.TraceWindowIsActive
  53. ; Tracer.EventFileSpec
  54. ; Tracer.FileWindowHandle
  55. ; Tracer.FileWindowIsActive
  56. ; Tracer.IsActive
  57. ;
  58. ;==============================================================================
  59. ;                           TRACER IMPLEMENTATION
  60. ;==============================================================================
  61.  
  62. Proc Tracer.Constructor()
  63.    Private
  64.       i
  65.  
  66.    ;-------------------------------------------------------------------------
  67.    ; Replace default high-level EventMan handler with trace handler...
  68.    ;-------------------------------------------------------------------------
  69.    Tracer_OriginalDispatchProc = EventMan.DispatchEventTagProc
  70.    EventMan.DispatchEventTagProc = "Tracer.TraceEvent"
  71.    ;-------------------------------------------------------------------------
  72.    ; Initialize variables...
  73.    ;-------------------------------------------------------------------------
  74.    Tracer.EventFileSpec = "Events.txt"
  75.    Tracer.FileWindowIsActive = False
  76.    Tracer.TraceWindowIsActive = False
  77.    ;-------------------------------------------------------------------------
  78.    ; Tracelist is used to write trace information to the event tracer canvas...
  79.    ;-------------------------------------------------------------------------
  80.    Array Tracer.TraceList[8]
  81.    For i From 1 to 8
  82.       Tracer.TraceList[i] = ""
  83.    EndFor
  84.    ;-------------------------------------------------------------------------
  85.    ; Control variables for the tracer procs...
  86.    ;-------------------------------------------------------------------------
  87.    Tracer_TraceTrigger = True
  88.    Tracer_TraceMouse = True
  89.    Tracer_TraceKeycode = True
  90.    Tracer_TraceMessage = True
  91.    Tracer_IsRecordOn = False
  92.    Tracer_EventNumber = 0
  93.    Dynarray Tracer_DefaultProcBag[]
  94.    Tracer_DefaultProcBag[-20] = "Tracer.ControlMenu" ; AltT
  95.    EventMan.SetHandlersFrom(Tracer_DefaultProcBag)
  96.    Tracer.IsActive = True
  97.  
  98. EndProc ; Tracer.Constructor
  99.  
  100. Proc Tracer.Destructor()
  101.    Message "Quitting."
  102.    If Tracer.FileWindowIsActive Then
  103.       Tracer.SaveAndCloseFile()
  104.    Endif
  105.    If Tracer.TraceWindowIsActive Then
  106.       Tracer.CloseEventTrace()
  107.    Endif
  108.    EventMan.DispatchEventTagProc = Tracer_OriginalDispatchProc
  109.    EventMan.ClearHandlersWith(Tracer_DefaultProcBag)
  110.    Release Vars
  111.       Tracer_OriginalDispatchProc,
  112.       Tracer.EventFileSpec,
  113.       Tracer.TraceWindowHandle,
  114.       Tracer.TraceWindowIsActive,
  115.       Tracer.FileWindowHandle,
  116.       Tracer.FileWindowIsActive,
  117.       Tracer.IsActive,
  118.       Tracer_DefaultProcBag,
  119.       Tracer_TraceProcBag,
  120.       Tracer_FileProcBag,
  121.       Tracer_TraceTrigger,
  122.       Tracer_TraceMouse,
  123.       Tracer_TraceKeycode,
  124.       Tracer_TraceMessage,
  125.       Tracer_IsRecordOn,
  126.       Tracer_EventNumber
  127.  
  128. EndProc ; Tracer.Destructor
  129.  
  130. ; ---------------------------- TRACER WINDOW --------------------------------
  131.  
  132. Proc Tracer.OpenEventTrace()
  133.    Private
  134.       WindowBag
  135.  
  136.    Dynarray WindowBag[]
  137.    WindowBag["Width"] = 38
  138.    WindowBag["Height"] = 10
  139.    If Tracer_IsRecordOn Then
  140.       WindowBag["Title"] = "Event tracer:Record is off"
  141.    Else
  142.       WindowBag["Title"] = "Event tracer:Record is off"
  143.    Endif
  144.    WindowBag["OriginRow"] = 13
  145.    WindowBag["OriginCol"] = 1
  146.    Window Create Attributes WindowBag To Tracer.TraceWindowHandle
  147.    Tracer.TraceWindowIsActive = True
  148.  
  149.    Dynarray Tracer_TraceProcBag[]
  150.    Tracer_TraceProcBag["Close"] = "Tracer.CloseEventTrace"
  151.    Tracer_TraceProcBag["Up"] = "Tracer.CallControlMenu"
  152.    Tracer_TraceProcBag["ObjectTag"] = StrVal(Tracer.TraceWindowHandle)
  153.    EventMan.SetHandlersFrom(Tracer_TraceProcBag)
  154.  
  155.    Return 1
  156. EndProc ; Tracer.OpenEventTrace
  157.  
  158. Proc Tracer.CloseEventTrace()
  159.    Private
  160.       CurrentWindow,
  161.       i
  162.  
  163.    CurrentWindow = GetWindow()
  164.    Window Select Tracer.TraceWindowHandle
  165.    Window Close
  166.    Tracer.TraceWindowIsActive = False
  167.    EventMan.ClearHandlersWith(Tracer_TraceProcBag)
  168.    For i From 1 to 8
  169.       Tracer.TraceList[i] = ""
  170.    Endfor
  171.    If IsWindow(CurrentWindow) Then
  172.       Window Select CurrentWindow
  173.    Endif
  174.    Return 1
  175. EndProc ; Tracer.CloseEventTrace
  176.  
  177. ;----------------------------- EVENTS.TXT WINDOW ----------------------------
  178.  
  179. Proc Tracer.OpenEventFile()
  180.    Private
  181.       WindowBag
  182.  
  183.    If IsFile(Tracer.EventFileSpec) Then
  184.       Editor Open Tracer.EventFileSpec
  185.    Else
  186.       Editor New Tracer.EventFileSpec
  187.    Endif
  188.    Tracer.FileWindowHandle = GetWindow()
  189.    Tracer.FileWindowIsActive = True
  190.  
  191.    Dynarray WindowBag[]
  192.    WindowBag["Width"] = 38
  193.    WindowBag["Height"] = 10
  194.    WindowBag["OriginRow"] = 13
  195.    WindowBag["OriginCol"] = 1
  196.    Window SetAttributes Tracer.FileWindowHandle From WindowBag
  197.  
  198.    Dynarray Tracer_FileProcBag[]
  199.    Tracer_FileProcBag["Close"] = "Tracer.CloseEventFile"
  200.    Tracer_FileProcBag["Up"] = "Tracer.CallControlMenu"
  201.    Tracer_FileProcBag[Asc("F2")] = "Tracer.SaveAndCloseFile"
  202.    Tracer_FileProcBag["ObjectTag"] = StrVal(Tracer.FileWindowHandle)
  203.    EventMan.SetHandlersFrom(Tracer_FileProcBag)
  204.  
  205.    Return 1
  206. EndProc ; Tracer.OpenEventFile
  207.  
  208. Proc Tracer.CloseEventFile()
  209.    Private
  210.       CurrentWindow,
  211.       Closed
  212.    CurrentWindow = GetWindow()
  213.    Window Select Tracer.FileWindowHandle
  214.    Closed = Tracer!CloseEditorWindow()
  215.    If Closed Then
  216.       Tracer.FileWindowIsActive = False
  217.       EventMan.ClearHandlersWith(Tracer_FileProcBag)
  218.    Endif
  219.    If IsWindow(CurrentWindow) Then
  220.       Window Select CurrentWindow
  221.    Endif
  222.    Return 1
  223. EndProc ; Tracer.CloseEventFile
  224.  
  225. Proc Tracer.SaveAndCloseFile()
  226.    Private
  227.       CurrentWindow
  228.    CurrentWindow = GetWindow()
  229.    Window Select Tracer.FileWindowHandle
  230.    Do_It!
  231.    Tracer.FileWindowIsActive = False
  232.    EventMan.ClearHandlersWith(Tracer_FileProcBag)
  233.    If IsWindow(CurrentWindow) Then
  234.       Window Select CurrentWindow
  235.    Endif
  236.    Return 1
  237. EndProc ; Tracer.SaveAndCloseFile
  238.  
  239. ;------------------------------ CONTROL MENUS -------------------------------
  240.  
  241. Proc Tracer.CallControlMenu()
  242.    If EventMan.MouseModeButton = "RIGHT" Then
  243.       Return Tracer.ControlMenu()
  244.    Else
  245.       Return 0
  246.    Endif
  247. EndProc
  248.  
  249. Proc Tracer.ControlMenu()
  250.    Private
  251.       Command,
  252.       ReturnCode
  253.    ShowPopup "Tracer" Centered
  254.       "Tr~a~ce settings":"Select events to record":"Trace",
  255.       Separator,
  256.       "~S~how traces":"Show traces window":"Show",
  257.       "~V~iew EVENTS.TXT":"View the EVENTS.TXT file":"View",
  258.       Separator,
  259.       "~Q~uit tracer":"Quit the event tracer":"Quit"
  260.    EndMenu
  261.    To Command
  262.    Switch
  263.       Case Command = "Esc":
  264.          ReturnCode = 1
  265.       Case Command = "Trace":
  266.          Tracer.TraceControl()
  267.          ReturnCode = 1
  268.       Case Command = "Show":
  269.          If Not Tracer.TraceWindowIsActive Then
  270.             Echo Off
  271.             Tracer.OpenEventTrace()
  272.             Echo Normal
  273.          Else
  274.             Message "TRACE window already on desktop..."
  275.          Endif
  276.          ReturnCode = 1
  277.       Case Command = "View":
  278.          If Not Tracer.FileWindowIsActive Then
  279.             Echo Off
  280.             Tracer.OpenEventFile()
  281.             Echo Normal
  282.          Else
  283.             Message "EVENTS.TXT already on desktop..."
  284.          Endif
  285.          ReturnCode = 1
  286.       Case Command = "Quit":
  287.          ShowMenu
  288.             "Cancel":"Do NOT close the event tracer",
  289.             "OK":"Close the event tracer"
  290.          To Command
  291.          If Command <> "OK" Then
  292.             ReturnCode = 1
  293.          Else
  294.             Tracer.Destructor()
  295.             ReturnCode = 2
  296.             Message "Tracer is unloaded."
  297.          Endif
  298.    EndSwitch
  299.    Return ReturnCode
  300. EndProc ; Tracer.ControlMenu
  301.  
  302. Proc Tracer.TraceControl()
  303.    Private
  304.       Control
  305.    ShowDialog "Set trace elements"
  306.       @5,20 Height 12 Width 33
  307.       CheckBoxes
  308.          @1,3 Height 5 Width 25 Tag "Traces"
  309.          "~T~rigger" To Tracer_TraceTrigger,
  310.          "~M~ouse"   To Tracer_TraceMouse,
  311.          "~K~ey"     To Tracer_TraceKeycode,
  312.          "Me~s~sage" To Tracer_TraceMessage,
  313.          "~R~ecord to EVENTS.TXT" To Tracer_IsRecordOn
  314.       PushButton
  315.          @8,2 Width 10 "~O~K" OK
  316.          Default
  317.          Value True
  318.          Tag "OK"
  319.          To Control
  320.       PushButton
  321.          @8,18 Width 10 "~C~ancel" Cancel
  322.          Value True
  323.          Tag "Cancel"
  324.          To Control
  325.    EndDialog
  326.    If Retval Then
  327.       If Tracer.TraceWindowIsActive Then
  328.          Dynarray WindowBag[]
  329.          If Tracer_IsRecordOn Then
  330.             WindowBag["Title"] = "Event tracer:Record is on"
  331.          Else
  332.             WindowBag["Title"] = "Event tracer:Record is off"
  333.          Endif
  334.          Window SetAttributes Tracer.TraceWindowHandle From WindowBag
  335.       Endif
  336.    Endif
  337.    Return 1
  338. EndProc ; Tracer.TraceControl
  339.  
  340. ;------------------------------ UTILITY PROCS -------------------------------
  341.  
  342. Proc Tracer!CloseEditorWindow()
  343.    Private
  344.       EditorWindow,
  345.       EventBag
  346.    EditorWindow = GetWindow()
  347.    WinClose
  348.    While IsWindow(EditorWindow) And (GetWindow() <> EditorWindow)
  349.       ;--------------------------------------------------------------
  350.       ; MenuChoice() could be used to detect presence of menu, ie. "No"
  351.       ; if the confirmation menu were up, and "Error" or "View" or "File"
  352.       ; or something if not, kind of mushy though, so I prefer the above.
  353.       ;--------------------------------------------------------------
  354.       ; ie. we're on a confirmation menu, so eat events until
  355.       ; accept or deny of winclose is indirectly confirmed. Accept is
  356.       ; detected by absence of EditorWindow; deny is detected by
  357.       ; return to the EditorWindow.
  358.       ;--------------------------------------------------------------
  359.       GetEvent to EventBag
  360.       ExecEvent EventBag
  361.    EndWhile
  362.    If IsWindow(EditorWindow) Then
  363.       Return false
  364.    Else
  365.       Return true
  366.    Endif
  367. EndProc ; Tracer!CloseEditorWindow
  368.  
  369. ; -------------------------- TRACE PROC -------------------------------------
  370.  
  371. ;----------------------------------------------------------------------------
  372. ; Requires EventMan.EventType, and EventMan.TriggerTag or EventMan.EventBag
  373. ;----------------------------------------------------------------------------
  374. Proc Tracer.TraceEvent()
  375.    Private
  376.       Action,
  377.       Active
  378.  
  379.    If EventMan.EventTag <> "IDLE" And
  380.       Not (EventMan.EventType = "MOUSE" and EventMan.MouseModeButton = "NONE")
  381.       Then
  382.       Tracer.TraceEventToWindow(EventMan.TargetWindow, EventMan.EventTag)
  383.    Endif
  384.    ExecProc Tracer_OriginalDispatchProc
  385.    Return EventMan.ReturnCode
  386.  
  387. EndProc ; Tracer.TraceEvent
  388.  
  389. ; ---------------------- WRITE EVENT TO TRACE WINDOW ------------------------
  390.  
  391. Proc Tracer.TraceEventToWindow(ObjectTag,EventTag)
  392.    Private
  393.       EventTrace,
  394.       i
  395.    If (Not Tracer.TraceWindowIsActive) And
  396.       (Not Tracer_IsRecordOn) Then
  397.       Return
  398.    Endif
  399.    Tracer_EventNumber = Tracer_EventNumber + 1
  400.    EventTrace = Format("W6",Tracer_EventNumber) + " " +
  401.                 Format("W6",ObjectTag) + " " + EventTag
  402.    If isAssigned(EventMan.IsWait) Then
  403.       EventTrace = EventTrace + "(" + StrVal(EventMan.TriggerCycle) + ")"
  404.    Endif
  405.    If Tracer.TraceWindowIsActive Then
  406.       SetCanvas Tracer.TraceWindowHandle
  407.       Canvas off
  408.       For i From 2 to 8
  409.          Tracer.TraceList[i-1] = Tracer.TraceList[i]
  410.       EndFor
  411.       Tracer.TraceList[8] = EventTrace
  412.       Clear
  413.       For i From 0 to 7
  414.          @i,0 ?? Tracer.TraceList[i+1]
  415.       EndFor
  416.       Canvas on
  417.    Endif
  418.    If Tracer_IsRecordOn And (Not Tracer.FileWindowIsActive) Then
  419.       Print File  Tracer.EventFileSpec
  420.          EventTrace + "\n"
  421.    Endif
  422. EndProc ; Tracer.TraceEventToWindow