Apple Carbon Programming. QuickStart.

AID: 2515
  • Status: Published

3410 points

  • Bypgnatyuk
  • TypeBest Practices
  • Posted on2010-02-22 at 13:46:00
Preface
 
Macintosh-1.jpg
  • 12 KB
  • First Mac
First Mac

In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system.
I've never seen this model. I did see this one:
 
Macintosh-2.jpg
  • 19 KB
  • Mac
Mac

I'm not that old. At that time I was a student. When I began to work as a Software engineer, we already had Intel 386, MS-Dos, Microsoft Windows 3.1. Since that I'm the biggest fun of Microsoft - Visual C++, MFC, ATL, COM, DCOM, COM+,.NET, C#,...

Few months ago I bought iPhone. Then Mac mini. I made my first applications for this phone. Then, for my Mac... Goodbye, Microsoft?

I think I found a very easy way to step in to the world of the Apple programming. In this short article I want to show how to begin, how to make a first Mac OS X program. I'm not going to scare anyone, I will not talk about Objective-C, I will not use Cocoa. These are very amazing things, but it is, obviously, not the easiest start.

We all began from the super language - our old and respectful C. From my point of view, it is extremely easy to begin programming for Mac OS X from Carbon - an old procedural 32bit API for developing Mac OS X application.

Project Hello

In case you have a Mac and Xcode installed on it, it will take 2 seconds to make a window application on Mac:

1. Create new project - File menu -> New Project...
2. Choose Carbon Application template in the Application section in the New Project wizard and press Choose button (in the bottom-right corner).
3. Set the project name and press Save button.
4. Press Build and Go.

Here are few images illustrating these steps above:
 
Xcode-New-Project.png
  • 76 KB
  • Xcode. New Project.
Xcode. New Project.

 
Save-As.png
  • 20 KB
  • Set project name
Set project name

 
It runs. The window knows how to minimize and maximize itself. The application has the standard Apple menu.

 
Hello.png
  • 20 KB
  • Application Hello
Application Hello


Source code

The application source code is in main.c file. Actually, everything from this file can be removed and replaced by the following code:

int main (int argc, char* argv[])
{
    IBNibRef        nibRef;
    WindowRef       window;
	
    // Create a nib reference to a nib file.
    CreateNibReference (CFSTR ("main"), &nibRef);
    SetMenuBarFromNib (nibRef, CFSTR("MenuBar"));
    CreateWindowFromNib (nibRef, CFSTR("MainWindow"), &window);
	
    // Dispose of the nib reference as soon as you don’t need it any more.
    DisposeNibReference (nibRef);

    // Make the unarchived window visible.
    ShowWindow (window);
	
    // Start the event loop. RunApplicationEventLoop is a
	// Carbon Event Manager function.
    RunApplicationEventLoop ();
	
    return 0;
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Select allOpen in new window



Now the program is much shorter - one C-style function (as it should be in the procedural language) with few lines of code:

1. Call CreateNibReference function to create the resource object. Call DisposeNibReference in order to release it.

2. Call SetMenuBarFromNib function in order to set the application menu.

3. Call CreateWindowFromNib to create the window from the resource.

4. Call ShowWindow to show the window.

5. Use RunApplicationEventLoop function to start the application event loop.

For a test purpose I can comment (or delete) three window related lines and the function will look like:

int main (int argc, char* argv[])
{
    IBNibRef        nibRef;
	
    CreateNibReference (CFSTR ("main"), &nibRef);
    SetMenuBarFromNib (nibRef, CFSTR("MenuBar"));
    DisposeNibReference (nibRef);

    RunApplicationEventLoop ();
	
    return 0;
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window



Now this code compiled and launched creates only the menu:
 
Menu.png
  • 43 KB
  • Only menu
Only menu

Let's remove everything:

int main (int argc, char* argv[])
{
    RunApplicationEventLoop ();
	
    return 0;
}
                                    
1:
2:
3:
4:
5:
6:

Select allOpen in new window


Build and Go:
Empty-Menu.png
  • 31 KB
  • Default menu
Default menu

I do remember very well how looks the minimal window application for Microsoft Windows.

Conclusion

It is a first and trivial experience in the Mac OS X development. But it gives an optimistic feeling, a confidence that the knowledge in C allows to study Mac OS, to make programs for Apple immediately, without long pauses for Objective-C and Cocoa books.

Reference

1. Apple Developer Connection. Carbon
2. Apple Developer Connection. Carbon Overview
3. Mac OS X Reference Library. Carbon Resources
4. Wikipedia: Carbon (API)
    Asked On
    2010-02-22 at 13:46:00ID2515
    Tags

    Carbon

    Topic

    Apple Programming

    Views
    2844

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top Apple Programming Experts

    1. Programmer-x

      86,352

      Master

      0 points yesterday

      Profile
      Rank: Guru
    2. pgnatyuk

      61,160

      Master

      10 points yesterday

      Profile
      Rank: Genius
    3. darbid73

      31,004

      0 points yesterday

      Profile
      Rank: Wizard
    4. un1x86

      16,200

      0 points yesterday

      Profile
      Rank: Master
    5. eoinosullivan

      14,715

      0 points yesterday

      Profile
      Rank: Genius
    6. jason1178

      7,600

      0 points yesterday

      Profile
      Rank: Genius
    7. SpeedyApocalypse

      6,950

      0 points yesterday

      Profile
      Rank: Wizard
    8. leuqarte

      6,800

      0 points yesterday

      Profile
    9. ishcabittle

      5,000

      0 points yesterday

      Profile
      Rank: Master
    10. Pra4444

      4,000

      0 points yesterday

      Profile
      Rank: Guru
    11. maximus5328

      4,000

      0 points yesterday

      Profile
      Rank: Wizard
    12. Tribus

      4,000

      0 points yesterday

      Profile
      Rank: Guru
    13. Anuroopsundd

      4,000

      0 points yesterday

      Profile
      Rank: Sage
    14. ZShaver

      4,000

      0 points yesterday

      Profile
      Rank: Master
    15. tampnic

      3,500

      0 points yesterday

      Profile
      Rank: Master
    16. sweetfa2

      3,200

      0 points yesterday

      Profile
      Rank: Wizard
    17. akahan

      3,000

      0 points yesterday

      Profile
      Rank: Sage
    18. Zoppo

      2,800

      0 points yesterday

      Profile
      Rank: Genius
    19. ahoffmann

      2,800

      0 points yesterday

      Profile
      Rank: Genius
    20. jkr

      2,800

      0 points yesterday

      Profile
      Rank: Savant
    21. Ray_Paseur

      2,800

      0 points yesterday

      Profile
      Rank: Savant
    22. breadtan

      2,800

      0 points yesterday

      Profile
      Rank: Genius
    23. Merete

      2,100

      0 points yesterday

      Profile
      Rank: Genius
    24. serialband

      2,000

      0 points yesterday

      Profile
      Rank: Master
    25. girionis

      2,000

      0 points yesterday

      Profile
      Rank: Genius

    Hall Of Fame