Thread overview
Hello Carbon World (Mac)
Dec 10, 2004
John Reimer
December 09, 2004
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...
December 10, 2004
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.
December 10, 2004
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