Advertisement
adilima

Mac OS X GUI App using plain C++

Dec 29th, 2012
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. /*
  2.  * test1.cpp
  3.  * This program shows how to access Cocoa GUI from pure C/C++
  4.  * and build a truly functional GUI application (although very simple).
  5.  *
  6.  * Compile using:
  7.  *   g++ -framework Cocoa -o test1 test1.cpp
  8.  *
  9.  * that will output 'test1' binary.
  10.  */
  11.  
  12.  
  13. #include <CoreFoundation/CoreFoundation.h>
  14. #include <objc/objc.h>
  15. #include <objc/objc-runtime.h>
  16. #include <iostream>
  17.  
  18. extern "C" int NSRunAlertPanel(CFStringRef strTitle, CFStringRef strMsg,
  19.                                CFStringRef strButton1, CFStringRef strButton2,
  20.                                CFStringRef strButton3, ...);
  21.  
  22.  
  23. int main(int argc, char** argv)
  24. {
  25.     id app = NULL;
  26.     id pool = objc_getClass("NSAutoreleasePool");
  27.     if (!pool)
  28.     {
  29.         std::cerr << "Unable to get NSAutoreleasePool!\nAborting\n";
  30.         return -1;
  31.     }
  32.     pool = objc_msgSend(pool, sel_registerName("alloc"));
  33.     if (!pool)
  34.     {
  35.         std::cerr << "Unable to create NSAutoreleasePool...\nAborting...\n";
  36.         return -1;
  37.     }
  38.     pool = objc_msgSend(pool, sel_registerName("init"));
  39.    
  40.     app = objc_msgSend(objc_getClass("NSApplication"),
  41.                        sel_registerName("sharedApplication"));
  42.    
  43.     NSRunAlertPanel(CFSTR("Testing"),
  44.                     CFSTR("This is a simple test to display NSAlertPanel."),
  45.                     CFSTR("OK"), NULL, NULL);
  46.    
  47.     objc_msgSend(pool, sel_registerName("release"));
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement