www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Hello Carbon World (Mac)

reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Speaking of GUIs...

For reference, here is what Hello World looks like
for the Carbon API of Mac OS X, ported over to D:

 import std.stdio;
 
 typedef void* CFStringRef;
 typedef void* IBNibRef;
 typedef void* WindowRef;
 
 alias int OSStatus;
 const OSStatus noErr = 0;

 CFStringRef CFSTR(char[] str)
 {
   return __CFStringMakeConstantString(cast(char *) str);
 }

 int main(char[][] args)
 {
     IBNibRef 		nibRef;
     WindowRef 	window;
     
     OSStatus		err;
 
     // Create a Nib reference passing the name of the nib file (without the
.nib extension)
     // CreateNibReference only searches into the application bundle.
     err = CreateNibReference(CFSTR("main"), &nibRef);
     if (err != noErr) goto CantGetNibRef;
     
     // Once the nib reference is created, set the menu bar. "MainMenu" is the
name of the menu bar
     // object. This name is set in InterfaceBuilder when the nib is created.
     err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
     if (err != noErr) goto CantSetMenuBar;
     
     // Then create a window. "MainWindow" is the name of the window object.
This name is set in 
     // InterfaceBuilder when the nib is created.
     err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window);
     if (err != noErr) goto CantCreateWindow;
 
     // We don't need the nib reference anymore.
     DisposeNibReference(nibRef);
     
     // The window was created hidden so show it.
     ShowWindow(window);
     
     // Call the event loop
     RunApplicationEventLoop();
 
     writefln("Goodbye from D");
 
 CantCreateWindow:
 CantSetMenuBar:
 CantGetNibRef:
 	return err;
 }

Compiles and runs just fine, with the proper extern(C) {} definitions and a GCC/GDC patched with framework support:
 gdc -o build/HelloWorld.app/Contents/MacOS/HelloWorld main.d -framework Carbon

Of course, real application use more than those 6 functions above :-) And adding a "Hello, World!" in InterfaceBuilder is probably cheating. --anders PS. The rest of the app was just "New Project > Carbon Application" (C) Xcode support for the GDC compiler is not yet fully completed...
Dec 09 2004
parent reply John Reimer <brk_6502 yahoo.com> writes:
Anders F Björklund wrote:

 Speaking of GUIs...
 
 For reference, here is what Hello World looks like
 for the Carbon API of Mac OS X, ported over to D:
 
 import std.stdio;
 
 typedef void* CFStringRef;
 typedef void* IBNibRef;
 typedef void* WindowRef;
 
 alias int OSStatus;
 const OSStatus noErr = 0;

 CFStringRef CFSTR(char[] str)
 {
   return __CFStringMakeConstantString(cast(char *) str);
 }

 int main(char[][] args)
 {
     IBNibRef                 nibRef;
     WindowRef        window;
     
     OSStatus         err;
 
     // Create a Nib reference passing the name of the nib file (without
     the .nib extension) // CreateNibReference only searches into the
     application bundle. err = CreateNibReference(CFSTR("main"), &nibRef);
     if (err != noErr) goto CantGetNibRef;
     
     // Once the nib reference is created, set the menu bar. "MainMenu" is
     the name of the menu bar // object. This name is set in
     InterfaceBuilder when the nib is created. err =
     SetMenuBarFromNib(nibRef, CFSTR("MenuBar")); if (err != noErr) goto
     CantSetMenuBar;
     
     // Then create a window. "MainWindow" is the name of the window
     object. This name is set in // InterfaceBuilder when the nib is
     created. err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"),
     &window); if (err != noErr) goto CantCreateWindow;
 
     // We don't need the nib reference anymore.
     DisposeNibReference(nibRef);
     
     // The window was created hidden so show it.
     ShowWindow(window);
     
     // Call the event loop
     RunApplicationEventLoop();
 
     writefln("Goodbye from D");
 
 CantCreateWindow:
 CantSetMenuBar:
 CantGetNibRef:
 return err;
 }

Compiles and runs just fine, with the proper extern(C) {} definitions and a GCC/GDC patched with framework support:
 gdc -o build/HelloWorld.app/Contents/MacOS/HelloWorld main.d -framework
 Carbon

Of course, real application use more than those 6 functions above :-) And adding a "Hello, World!" in InterfaceBuilder is probably cheating. --anders PS. The rest of the app was just "New Project > Carbon Application" (C) Xcode support for the GDC compiler is not yet fully completed...

Looks fun! Is it common practice in Carbon programming to use all those gotos. That seems strange.
Dec 09 2004
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
John Reimer wrote:

For reference, here is what Hello World looks like
for the Carbon API of Mac OS X, ported over to D:

Is it common practice in Carbon programming to use all those gotos. That seems strange.

Common and common... It is just what the sample code used ? It avoids nesting if's, but try/catch is somewhat neater :-) --anders
Dec 09 2004