Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

macos

cocoa

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?

like image 867
Bill Avatar asked Dec 23 '25 02:12

Bill


1 Answers

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).

like image 179
justin Avatar answered Dec 24 '25 17:12

justin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!