Friday, September 23, 2011

CFUserNotification With 3 Buttons

Here's a sample to display a MessageBox with more than 1 button, and how to get the user response.


/***
 * test4.c
 * CFUserNotification Dialog with 3 buttons
 * 
 * Compile with the following commands:
 *   gcc -framework CoreFoundation -o Test4 test4.c
 *   mkdir -pv Test4.app/Contents/MacOS && mv -v Test4 Test4.app/Contents/MacOS
 *
 */


#include <CoreFoundation/CoreFoundation.h>

int main(int argc, const char** argv)
{
CFOptionFlags cfRes;
CFStringRef strTest = CFSTR("Test Button");
CFUserNotificationDisplayAlert(5, kCFUserNotificationNoteAlertLevel
NULL, NULL, NULL
CFSTR("Testing"), 
CFSTR("Click on any button..."), 
CFSTR("OK"), 
CFSTR("Cancel"), 
CFSTR("Test Button"), 
&cfRes);
switch (cfRes)
{
case kCFUserNotificationDefaultResponse:
strTest = CFSTR("Default response");
break;
case kCFUserNotificationAlternateResponse:
strTest = CFSTR("Alternate response");
break;
case kCFUserNotificationOtherResponse:
strTest = CFSTR("Other response");
break;
default:
strTest = CFSTR("Cancel response");
break;
}
CFUserNotificationDisplayNotice(0, kCFUserNotificationPlainAlertLevel
NULL, NULL, NULL, CFSTR("Result"),
                                        strTest, CFSTR("OK"));
return 0;
}

I intentionally used 5 seconds timeout for parameter 1 (argument 0) to call CFUserNotificationDisplayAlert(), so that we can see more differences than previous examples. If we pass 0 for this parameter, then the Dialog Box will never be dismissed automatically, unless the user click one of the buttons.

The 3 NULL parameters above are for the address of iconFileURL, soundFileURL, and localizedStringURL. We don't use iconFileURL because we use the icon from system through the second parameter kCFUserNotificationNoteAlertLevel, which display a balloon with 'I'. The other ones are 'Stop', 'Caution', and 'Plain' --- AlertLevel. Try to change it yourself and recompile you code.

Check out Mac OS X official documentations for this.


No comments:

Post a Comment