# Using Callback Functions to Respond to User Interface Events

Callback functions respond to all events generated by the User Interface Library. The C prototypes for the callback functions are in userint.h. You can have callback functions for panels, menu bars, controls, or the main callback. When the user generates an event on a particular user interface object, the appropriate callback function executes. Idle events and end-task events are passed to the main callback function only. Refer to InstallMainCallback for more information.

LabWindows/CVI passes event information from the GUI to your callback functions. For example, callback functions receive the type of user interface event that occurred, such as EVENT_LEFT_CLICK, and some additional information concerning that event, such as the x- and y-coordinates of the mouse cursor when the click occurred. You, as the developer of these callback functions, are free to use this information when responding to events. LabWindows/CVI also passes callback data to the callback function that you define.

A top-level panel callback receives the EVENT_CLOSE message callback when the user executes the Close command from the System menu or clicks on the Close button in the upper right corner of the panel title bar.

A panel callback receives the EVENT_PANEL_SIZE and EVENT_PANEL_MOVE messages when the user resizes or moves the panel. The panel callback does not receive these messages when you programmatically resize or move a panel.

The main callback receives the EVENT_END_TASK message when the user tries to shut down Windows or when the user tries to terminate your application, for example, when the user executes the Close command from the task bar button for your application.

The tutorial in the Getting Started with LabWindows/CVI manual presents examples of callback functions. Many of the sample programs that come with LabWindows/CVI illustrate callback functions, too. The following diagram and example pseudo-code illustrates the callback function concept.

Callback Function Concept

panel_handle = LoadPanel(...);
DisplayPanel(panel_handle, ...);
menu_handle = LoadMenuBar(...);
RunUserInterface()

int CVICALLBACK PanelResponse (int handle, int event, void *callbackdata, int eventdata1, int eventdata2)

{

switch (event) {

case EVENT_PANEL_SIZE :

. /* Code that responds to the panel */
. /* being resized */
break;

case EVENT_PANEL_MOVE :

. /* Code that responds to the panel */
. /* being moved */
break;

case EVENT_KEYPRESS :

. /* Code that responds to a keypress */
. /* eventdata1 & eventdata2 contain */
/* keycode information */
break;

}

return(0);

}

int CVICALLBACK ControlResponse (int handle, int control, int event, void *callbackdata, int eventdata1, int eventdata2)

{

if (control == PANEL_CONTROL1) {

switch (event) {

case EVENT_RIGHT_CLICK :

. /* Code that responds to a right */
. /* click on CONTROL1 */
break;

case EVENT_VAL_CHANGED :
. /* Code that responds to a value */
. /* change on CONTROL1 */
break;

case EVENT_COMMIT :

. /* Code that responds to a commit */
. /* event on CONTROL1 */
break;

}

}

if (control == PANEL_CONTROL2) {

switch (event) {

case EVENT_RIGHT_CLICK :

. /* Code that responds to a right */
. /* click on CONTROL2 */
break;

case EVENT_COMMIT :

. /* Code that responds to a commit */
. /* event on CONTROL2 */
break;

}

}
return(0);

}

int CVICALLBACK MenuBarResponse (int menubar, int menuitem, void *callbackdata, int panel)

{

switch (menuitem) {

case MENUBAR_MENU1_ITEM1:

. /* Code that responds to ITEM1 in */
. /* MENU1 of the menu bar. */
break;

case MENUBAR_MENU1_ITEM2:

. /* Code that responds to ITEM2 in */
. /* MENU1 of the menu bar. */
break;

}
return(0);

}

| Note If you assign callback functions to your GUI objects using the User Interface Editor, LoadPanel and LoadMenuBar automatically install these functions. Otherwise, you must install them programmatically through the callback installation functions (InstallPanelCallback, InstallCtrlCallback, InstallMenuCallback, and others).

| Note Do not call longjmp from within a callback function.

The CVICALLBACK macro should precede the function name in the declarations and function headers for all user interface callbacks. This ensures that the functions are treated by the compiler as cdecl, even when the default calling convention is stdcall. CVICALLBACK is defined in cvidefs.h, which is included by userint.h. The CVICALLBACK macro is included where necessary in the header files generated by the User Interface Editor and in source code generated by CodeBuilder.

For detailed information about the user interface callback functions, refer to the descriptions of PanelCallbackPtr, CtrlCallbackPtr, MenuCallbackPtr, MenuDimmerCallbackPtr, MainCallbackPtr, and DeferredCallbackPtr.