Delegates and events

The Quartex Pascal dialect supports traditional object pascal events, the same that you find in Delphi, Freepascal, C and C++. These are used throughout the runtime-library for a plethora of reasons, and some widgets expect you to manually write event handlers for particular events.

The event mechanism common for native languages is mostly referred to as a ‘callback’ or ‘procedural event’. Besides this classical event type, modern JavaScript mostly operate with something called delegates.

A delegate is an object that represents an event, to which you can attach an infinite amount of procedural handlers. When an event fires, the delegate-object will invoke all the handlers attached to it in the same sequence as they were attached. The majority of JavaScript objects and visual elements deal with delegates, and only support the classical procedural event types for backwards compatibility.

Above: When you select a widget, you can add as many delegates as you like to that widget from the Delegates tab

The rationale for this has to do with how HTML documents deal with events, because in the DOM (document object model) events bubbles upwards until handled. If you click a button on a panel, the event actually fires on the background first, then on the panel and finally on the button.

This makes sense when you realize that you can have animations executing on multiple layers (like a Netflix menu which scroll both horizontally and vertically at the same time), layers which all need feedback from touch, mouse or pointer movement at the same time.

Easy to use

The Quartex IDE makes it very easy to deal with delegates. You can create and attach as many consumer delegates as you like to a widget. If you need 10 “OnClick” event handlers, that is not a problem. I have done my best to represent delegate consumers as close to Delphi as I could; in that you can select a widget, switch to the delegates tab, click the ‘Add’ button, pick the delegate you need – and the IDE will automatically insert a handler procedure for you. The IDE will also create the delegate consumer object automatically as a part of the constructor, so you don’t really have to think that much about it.

Without going off on a tangent, JavaScript operates with the concept of event providers and event consumers. Needless to say, the first is an object that represents an event, while the other is an object that encapsulates a handler. To an object pascal developer this obsession with encapsulation can sometimes seem wasteful, but there are some benefits once you understand the HTML / Node.js ecosystem.

Widget developers can also create their own delegate provider objects via TQTXCustomDelegate.

Read more