Resources | Resources |



Creating a model listener for a button

When a button is pressed, the application needs to know that this event has taken place.

To be notified that a button has been pressed, or has been changed in some way, the application creates a model listener callback function, initializes the listener, and attaches the listener to the view model for the button.

When the button is pressed, the callback function is called and can take action based on the event that occurred. For information on listener callback functions, see Creating a listener callback function.

c_simple_widget_app_CreateButton() attaches a model listener to a view model as follows:

  1. c_simple_widget_app_CreateButton() first gets the view model for the button:
    if (AEE_SUCCESS != IWidget_GetViewModel(pMe->piwButton,
                &piButtonViewModel)) {
        return FALSE;
  2. It then initializes a ModelListener data structure for the listener using the LISTENER_Init() helper function.

    The second parameter to LISTENER_Init() is the name of the listener callback function and the third parameter is the data that will be passed to the listener callback function. In this case, the data for the listener callback function is the pMe pointer, which allows the callback function to access the data in the applet structure.

                c_simple_widget_app_ButtonListener, pMe);
  3. The function then calls IModel_AddListener() to attach the listener to the view model of the button, as follows:
    if (AEE_SUCCESS != IModel_AddListener(piButtonViewModel,
                &pMe->ButtonChanged)) {
        DBGPRINTF("attach listener failed");
        return FALSE;
  4. The view model pointer is not needed any longer because the button has a reference to its view model, so it is released: