11-03-2015 03:51 PM
OK, this is a dumb question, but generally speaking, what do you use the callbackData for ?
Yes, I know I can use:
SetCtrlAttribute (panel, control, ATTR_CALLBACK_DATA, &SomeStaticData);
And recover its value inside the callback function with:
typeofmydata Val=*(typeofmydata*)callbackData;
But except sometimes in the case of deferred callbacks, I don't see a use for it.
11-04-2015 02:14 AM - edited 11-04-2015 02:18 AM
I use callbackData parameter on settings panels: controls on that panel are all set with callbackData pointed to the save button, which initially is dimmed. When the user modifies a control, the associated callback enables the save button, so that I know that modifications have made and can trap this occurrence on the exit button callback. This way I can set a single function for all controls on the panel.
Some code will explain this paradigm better. When I load a settings panel I initialize all controls this way:
int CVICALLBACK loadSettingsPanel (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int tmpH, aryH;
if (event != EVENT_COMMIT) return 0;
// Load panel
tmpH = LoadPanel (0, UIR, PANEL);
if (tmpH < 0) {
// Warn the user for error
goto Error;
}
// Initialize controls
SetCtrlAttribute (tmpH, PANEL_NUMERIC, ATTR_CALLBACK_DATA, (void *)PANEL_SAVEBTN);
SetCtrlAttribute (tmpH, PANEL_STRING, ATTR_CALLBACK_DATA, (void *)PANEL_SAVEBTN);
// Repeat for all controls...
// In case controls are grouped into arrays things are simpler:
aryH = GetCtrlArrayFromResourceID (tmpH, CTRLARRAY);
SetCtrlArrayAttribute (aryH, ATTR_CALLBACK_DATA, (void *)PANEL_SAVEBTN);
// Initialize 'Exit' button
SetCtrlAttribute (tmpH, PANEL_QUITBTN, ATTR_CALLBACK_DATA, (void *)PANEL_SAVEBTN);
// Other stuff here...
// Show pannel
InstallPopup (tmpH);
Error:
return 0;
}
(I know I could iterate on all controls in a panel but I don't know whether this parameter can be set for all control types -including decorations and so on- so to avoid problems I manually set it for active controls only).
This is the generic callback which can be associated to whichever control type you want:
int CVICALLBACK valueModified (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
if (event != EVENT_VAL_CHANGED) return 0;
// Enable 'Save' button
if (callbackData) SetInputMode (panel, (int)callbackData, 1);
return 0;
}
This is the exit button callback:
int CVICALLBACK exitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int dimmed;
if (event != EVENT_COMMIT) return 0;
if (callbackData) {
GetCtrlAttribute (panel, (int)callbackData, ATTR_DIMMED, &dimmed);
if (!dimmed) {
// Ask the user if he wants to save changes: if yes return 0
}
}
DiscardPanel (panel);
return 0;
}
The save button callback must dim itself on success to prevent further confirm popups on exit.
I find this framework handy since it permits to have a single callback for all similar control even on different panels.