Lomse library. API documentation
0.30.0
|
Lomse is platform independent code, and knows nothing about your platform mechanisms for creating events. For this reason, all Lomse communication with the user application takes place through notifications, implemented by invoking a callback function in your application.
Lomse notifications can be divided into two categories based on how they are created and how they are processed:
For managing events, Lomse architecture uses the observer pattern: any other object wanting to know about events must register as an observer by providing an event handler object or a callback method/function for handling the events. In theory, your application could register a handler for each event type wanting to receive.
But in practice, you must be aware that the name event could be misleading. Lomse is not an event driven library, but a collection of services that run in the user application thread from which the service is requested. While processing a service request Lomse could find important things to communicate to your application. For this Lomse will invoke a callback so that you can take note, but Lomse needsto continue processing the service request. Therefore:
Due to this, it was decided to send all events to a single event handler, instead of using the subscription mechanism. The rationale was that a single handler would simplify the user application task of creating an OS/application event, and putting it in the application events loop. This handler, named the global handler for events, is set up at Lomse initialization:
Unfortunately, currently not all events are sent to this handler. As I was developing the library I forgot the reasoning for having created the global handler and thus, for some events created later I maintained the subscription mechanism of the observer pattern. During the library tests, with the LenMus Phonascus application, there were no problems with these events by not having decoupled its processing in the user application. And so, the subscription mechanism remained active for these events.
As a consequence, the situation is now a little bit confusing: some events are always sent to the global handler but other events will not be received unless your application register an specific event handler for each one of these event types. Of course you can always register the global handler as the handler for these events, but you will have to code it in your application.
With the exception of the k_update_window_event
it is not mandatory to handle all other Lomse events.
Here is a list of all events, grouped by handling mechanism. For more information on each event read the class documentation for the event.
a) Events always sent to the global handler:
k_update_viewport_event
) - Ask user app to update viewport origin using provided coordinates.k_end_of_playback_event
) - End of playback.k_highlight_event
) - Event containing mainly a list of notes/rests to highlight or unhighlightk_on_link_clicked_event
) - left click on link (ImoLink object).k_show_contextual_menu_event
) - right click on object: contextual menu requestk_selection_set_change
) - Selected objects changedk_pointed_object_change
) - Cursor pointing to a different objectb) Events for which you will have to register a handler at the event creator object (see How to register an event handler):
k_update_window_event
) - Ask user app to update window with current bitmap. k_do_play_score_event
) - Start/resume playbackk_pause_score_event
) - Pause playbackk_stop_playback_event
) - Stop playback k_control_point_moved_event
) - User moves a handler: handler released event k_mouse_in_event
) - Mouse goes over an objectk_mouse_out_event
) - Mouse goes out from an objectk_on_click_event
) - Document, ImoContentObj: click on object To capture and handle an event you must register an event handler
on the object generating the events by invoking its add_event_handler()
method. Your handler can be:
All Lomse objects that generate events derive from lomse::Observable class. Currently, events can be generated by the following objects:
event handler
on the Interactor.The parameters for the add_event_handler()
method depends on the type of handler method. There are three possibilities:
a) The handler is a C function:
b) The handler is a C++ method:
c) The handler is a C++ object derived form lomse::EventHandler:
The parameters for these methods are:
eventType
is the event type you wish to handle, such as a mouse click.SpEventInfo
is an shared pointer to the event object. All events derive from lomse::EventInfo
class.For the C function case:
pt2Func
is a pointer to the C function that will handle the event. It expects only one parameter: a shared pointer to the Event object.For C++ method case:
pThis
is a pointer to the object that will handle the event.pt2Func
is a pointer to the member method that will handle the event. It must be an static method. It will receive as first parameter the pointer to the object, so that you can invoke non-static methods if necessary.And for the C++ object case:
pHandler
is a pointer to an object derived from EventHandler that will handle the event. It must implement method handle_event()
.Example:
It is mandatory to handle Lomse requests. For this, you have to set up a callback method (at library initialization). For instance:
Lomse will make any necessary request by invoking this callback method. Therefore, requests are always handled in a single point in your application.
When your callback method is invoked, it will receive as parameter an object derived from class Request, containing the information about the required information or platform dependent service. And Lomse process is paused until the user application provides the requested data.
The list of possible Requests is:
(dynamic)
element has been found. Lomse is requesting user application for the dynamic content that must be inserted.