Bill
Bill

Reputation: 305

Simple Alert Box in C++, not Objective-C

I need a very simple alert box like NSRunAlertPanel(), but this is for a C++ embedded 64-bit app and I don't know Cocoa nor Objective-C.

Any ideas on how to do this?

Upvotes: 3

Views: 1227

Answers (1)

justin
justin

Reputation: 104698

Option A

One solution would be to look at the CFUserNotification APIs.

Option B

Another option would be to wrap NSRunAlertPanel, using CoreFoundation types as parameters:

// MONNSRunAlertPanel.hpp
extern "C++" {
int MONNSRunAlertPanel(CFStringRef title,
                       CFStringRef msg,
                       CFStringRef defaultButton,
                       CFStringRef alternateButton,
                       CFStringRef otherButton);
}


// MONNSRunAlertPanel.mm

#include <Foundation/Foundation.h>
#include "MONNSRunAlertPanel.hpp"

int MONNSRunAlertPanel(CFStringRef title,
                       CFStringRef msg,
                       CFStringRef defaultButton,
                       CFStringRef alternateButton,
                       CFStringRef otherButton) {
    int result = 0;
    @autoreleasepool {
        result = NSRunAlertPanel(
            (NSString *)title,
            (NSString *)msg,
            (NSString *)defaultButton,
            (NSString *)alternateButton,
            (NSString *)otherButton
            );
    }
    return result;
}

If you want a C symbol, name the file MONNSRunAlertPanel.m (objc), and alter the header accordingly,

If you want it as a C++ symbol, just name the file MONNSRunAlertPanel.mm (objc++).

Assuming you're using the default compiler setting, our source will be compiled based on its file extension.

Finally, add Foundation and AppKit to link to the necessary system libraries.

Then you can call MONNSRunAlertPanel without dragging Foundation.framework into your C++ sources (because CFString and NSString types are bridged).

Upvotes: 2

Related Questions