6.wxWidgets – Event Handling

OVERVIEW

To make a GUI work, we need Events. Events are generated when a user interacts with the GUI or some system processes are run or executed. Without event handling, a GUI will remain static and not be functional.

wxWidgets run an event loop which polls for any event that occurs. The event loop runs continuously and only stops when the program is terminated. When an event occurs, an event dispatcher is invoked. The event dispatcher sends the event data to an event handler. An event handler is a system-defined or a user-defined function which handles the event.

Through the evolution of wxWidgets, event handling has been done in three different ways. The first way is called Static event handling because the mappings between the events and event handlers have to be defined during compile time. The second and third ways are called Dynamic event handling because they can be defined at runtime as well as compile-time.

  • Using an Event Table – In this, an event lookup table is defined statically in code. Each item in the table maps an event to an event handler. When an event occurs, wxWidgets looks up the event table to see which handler should the event dispatcher call for the particular event
  • Using Connect – This is an improvement over using an Event Table, in the sense that the code to connect an event to an event handler can be put in multiple places and not all in one single place as in an Event Table. Connect() can be used within a method or a method and not have to be globally declared as an Event Table.
  • Using Bind – This builds more flexibility and safety over Connect(). Bind can bind an event to a method from a different class or object altogether.

wxWidgets recommends using Bind() as far as possible, but you are free to use any of the methods as long as you stick to one way of handling events.

Be the first to comment

Leave a Reply

Your email address will not be published.


*