Providing event handlers in macros

Providing event handlers in macros

While running, CorelDRAW raises various events to which macros can respond through the use of event handlers — subroutines with specific, defined names within a ThisMacroStorage module in CorelDRAW. Every CorelDRAW macro project (that is, GMS) file has one ThisMacroStorage module within its CorelDRAW X4

Objects subfolder for the project. (Similarly, every Corel PHOTO-PAINT macro project has one ThisDocument module within its Corel PHOTO-PAINT X4 Objects subfolder for the project.)

The GlobalMacroStorage object is a virtual object that represents each and all open CorelDRAW documents. The GlobalMacroStorage object has several events that are raised at the time of any event, such as opening, printing, saving, or closing a CorelDRAW document (although the range of events is actually greater than this

because each one has a “before” and “after” event). To respond to an event, you must provide an event handler — a subroutine in any ThisMacroStorage module

with a specific name for which CorelDRAW is pre-programmed to search. However, CorelDRAW does check all ThisMacroStorage modules in all installed projects, so you can create an event-driven solution and distribute it as a single project file just as you would provide any other form of solution. Each project can have only one ThisMacroStorage module, and it is automatically created when the project is first created.

For example, a solution may need to respond to a document closing by logging the closure in a file as part of a workflow-management and recording system. To respond to a document opening, the solution must respond to the GlobalMacroStorage class’s OpenDocument event. To do this, open a ThisMacroStorage module for

editing in the Macro Editor; next, choose GlobalMacroStorage from the Object list box at the top of the Code editing in the Macro Editor; next, choose GlobalMacroStorage from the Object list box at the top of the Code

need only write some code that adds the name of the opened file to the log. The recommended way to do this is to create a public sub in a different module that actually does the work, such that the event handler simply calls that sub when required; this keeps the size of the ThisMacroStorage module as small as possible, so that the run-time interpreter can more easily parse all the ThisMacroStorage modules each time an event is raised. The following code illustrates this example:

Private Sub GlobalMacroStorage_OpenDocument( ByVal Doc As Document, _ ByVal FileName As String)

Call LogFileOpen(FileName)

End Sub Here is a small sample of the events available in CorelDRAW:

Raised when the user starts CorelDRAW

DocumentNew Raised when a document is created; passes a reference to the document DocumentOpen

Raised when a document is opened; passes a reference to the document DocumentBeforeSave

Raised before a document is saved; passes the file name of the document as a parameter DocumentAfterSave

Raised after a document is saved; passes the file name of the document as a parameter DocumentBeforePrint

Raised before the Print dialog box is displayed

DocumentAfterPrint

Raised after a document is printed

SelectionChange

Raised when a selection changes

DocumentClose

Raised before a document is closed

Quit

Raised when the user quits CorelDRAW

Event handlers for frequent events — such as events related to the Shape class — should be as efficient as possible, to keep the application running as quickly as possible.