VisualWorks Smalltalk is a portable Smalltalk implementation that runs on many Unixes, Mac and Windows. All that changes between platforms is the virtual machine, and this is the interface between the host environment and the Smalltalk environment. In the case of UI events, the VM handles all events for all the Smalltalk windows.
Smalltalk in VisualWorks uses green threads. That is, from the operating system point of view there is only one real thread executing Smalltalk code. This makes the system nicely deterministic.
All UI events (mouse, keyboard and damage) flow into the Smalltalk system via a single instance of the class InputState. For each event, the InputState is invoked by the VM and is given a window handle (which maps to a Window instance in the image) and an Array which contains the details of the event.
Each event needs to get to the Smalltalk code which represents the window as seen by a user. To see how the event gets there we need to understand a little of how windows work in VisualWorks. Here are the key players and what they do:
- ApplicationModel: Programmers developing a GUI in VisualWorks make subclasses of ApplicationModel. These classes contain the definition of the window (often specified as a "canvas" that is created using a graphical window painter tool). The ApplicationModel subclass is responsible for defining the interface and performing all the operations on the model that a user of the GUI would expect.
- Builder: When an ApplicationModel wants to display it's window, it creates an instance of Builder. The builder takes the window specification and creates objects that map to all the widgets to be displayed on the window.
- Application Window: The builder creates an instance of Application Window which represents the window as known to the host operating system. It is this object that is referred to by the VM passing a new UI event to the InputState.
- WindowManager: Every ApplicationWindow must be registered with exactly one WindowManager. The WindowManager may contain any number of ApplicationWindows.
- EventSensor: There is an EventSensor for every ApplicationWindow. The Event Sensor is responsible for converting the event data in Array form into an instance of Event (well, a subclass of Event).
- Event: There is a subclass of Event for each different kind of event that can come in via an Event Sensor. For example a KeyPressedEvent or a MouseMovedEvent. Each event knows it's value (e.g. character, or mouse location) and the ApplicationWindow to which it applies.
Now we have all the players in the game, lets have a look at how things unfold. We assume that an ApplicationModel has been started and that the Builder has created the ApplicationWindow and set it up to run.
- EventQueue. Every WindowManager has a single EventQueue. Events are added to the queue by the EventSensors, and are removed and acted upon by the WindowManager. The Window Manager passes the Event on to the subject ApplicationWindow.
The following happens in a process called the "event loop"
There is also one process for each WindowManager in which the WindowManager loops pulling events out of the queue. It goes like this:
- The VM receives an event from the OS
- The VM passes the event Array and ApplicationWindow to the InputState
- The InputState asks the subject ApplicationWindow for the EventSensor and passes the event to it.
- The EventSensor creates an instance of Event corresponding to the values in the Array passed in by the VM
- The EventSensor asks the ApplicatioWindow to add the new event to the EventQueue of it's WindowManager.
- The WindowManager pulls the next Event from it's EventQueue
- The WindowManager identifies the ApplicationWindow which is the subject of the Event.
- The WindowManager hands the Event to the ApplicationWindow for action
- The ApplicationWindow calls the logic in the ApplicationModel which corresponds to the Event (e.g. a button being clicked, or a character being entered).
- Once the Event has been completely handled, the ApplicationWindow returns control to the WindowManager which starts the loop again