(If you do not see the frame version of this page with a table of contents in the left frame, click here.)

farSlang Event Handlers

You can write a farSlang module to perform as an ordinary program operating in the farVIEW environment. You execute such a module using the farSlang/Run menu item. But you can also use farSlang procs as farVIEW event handlers. You can also combine the two modalities in a single farSlang module. This discussion will attempt to explain how.

You can write farSlang event handler procs to modify the operation of  topics and farVIEW windows. You have two ways to link event handlers to window events: use the <script> tag in a farVIEW form, or write farSlang code to associate an event with a farSlang proc using the CWindow.SetEventProc API. Let's look at a simple example of the latter.

The following example farSlang module creates a window containing a button. The module links the onClick event of the button to an event handler proc, called clicker, within the module. When you click the button, the event handler proc sounds a beep. We'll include a frame for the window so that you can close it using the Xit-box in the upper-right corner of the window.
 

module example
   glb System: CSystem

   -- Event handler to beep when the button is clicked
   proc clicker?
      beep
      return true
      endProc clicker

   -- Initialization code begins here

   -- Create the main window and its button
   var rect := CRect(100, 100, 320, 240)
   var window := CWindow(nil, rect, FWW_FRAME, 'Example')
   var zero := CRect(0, 0, 0, 0)
   var button := CButton(window, zero, FWW_ATCENTER, 'Click Me')

   -- Link the button onClick event to the clicker proc
   window.SetFarSlang(System.FarSlang)
   window.SetEventModule('example') -- name of this module
   window.SetEventProc(button, onClick, 'clicker')

   -- Make the window visible
   window.Open
   endModule example

I should note that the clicker event handler proc does not need to be in the example module, it could be in any farSlang module. However, the farVIEW implementation does require that all the event handlers for a given window tree must reside in a single module.

Here is the same example as a XUL script with a farSlang event handler:

<?xml version="1.0"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script language="text/farSlang">
    proc clicker
      beep
      endProc clicker
  </script>
  <button label="Click Me" onclick="clicker"/>'
</window>

Here is a table of the farVIEW events that you can link to farSlang window event handlers:
 

Event Name
When the event occurs
onAbort
the Abort button is clicked
onBlur
the window loses focus
onCancel
the Cancel button is clicked
onClick
a button is clicked
onClosing
the window is closing
onCommandKey
a command key is pressed (Fun, Alt, or Ctrl)
onDefButton
the default button is clicked
onDoubleClick
the LMB is double clicked
onEnter
the Enter key is pressed
onFocus
the window gains focus
onIgnore
the Ignore button is clicked
onKeyDown
a key is enetered
onLMBDown
the LMB is pressed
onLMBUp
the LMB is released
onMouseEnter
the mouse enters the window
onMouseLeave
the mouse leaves the window
onMouseMove
the mouse moves within the window
onNo
the No button is clicked
onOk
the Ok button is clicked
onOpen
the window is opening
onRetry
the Retry button is clicked
onReturn
the Return (Enter) key is pressed
onRMBDown
the RMB is pressed
onRMBUp
the RMB is released
onSetToOn
a checkbox or radiobutton is set to on
onYes
the Yes button is clicked