Carbon Programming Tutorial Demos

 

These demos provide an introduction to Carbon application programming on the Mac.   They are designed to be studied in order and most demos build upon concepts and routines introduced in previous one(s.)  These demos are a result of spending a lot of time trying to learn Mac OS X programming from scratch wading through a lot of Apple’s documentation, 3rd Party books, C and Pascal sample code by Apple and others and obtaining information directly from helpful individuals.   As these simple demos show, even a hobbyist or novice programmer can create some very interesting and visually appealing applications on the Mac.  Apple has provided a very nice (and very large) toolbox.  Hopefully, these demos will decrease the learning curve and provide a roadmap for novice, hobbyist and student programmers to write applications for the Mac.


I decided to go with Pascal and Carbon framework instead of the heavily promoted Objective C and Cocoa framework since one must immerse oneself and go through a lot more preliminary study before a simple Hello World program can be written with the latter.  As can be seen in Demo1, Carbon does not require much custom source code overhead.  Although the Carbon framework is not simple, it is much easier to implement than Cocoa.   Cocoa is Apple’s preferred framework however. 


These tutorial demos are written in the Pascal programming language (using the multi platform public domain Free Pascal compiler or FPC.) However, most of the actual code is made up of Carbon library calls and constants which, are for the most part, syntactically the same as what you will find in the C library supplied by Apple.  Thus, these tutorials should easily be understood and utilized by non Pascal programmers.


I have tried to use the latest release versions of the Free Pascal Compiler (FPC), Lightweight IDE (from Ingemar Ragnemalm), Interface Builder (from Apple), as well as the versions of  Mac OS X for both the Intel and PPC platforms.   I will periodically update all the demos as new versions  of the OS and development tools come out but can't promise I will be up to date all the time (or ever.)   See the source code header comments for specific information.


Note 1: {July 5, 2008} All demos currently compile and run on both Intel (Leopard 10.5.4) and PPC (Tiger 10.4.11) Mac Mini cpus except for Demo 7 which does not act correctly under Tiger for some reason.   If you notice any bugs, things that are not clear or suggestions for improvement of any kind, I would appreciate your feedback.


Note 2:  {July 9, 2008} To get MacOSAll  to compile and link successfully with the FPC 2.2.1 (and later) release see the fix included at the end of this document. 
MacOSAll FixMacOSAll_Fix.html


Note 3:  {July 26, 2008} To get the applications to run under FPC 2.2.2 Intel, the math unit needed to be added and floating point exception flags explicitly set so the apps won't crash inside RunAppEventLoop.   This seems to be related to a possible bug in Apple's Carbon routines interacting with FPC.



Demo1.p


Draws a Pascal string "Hello World" in a window.   Demonstrates (slightly more than) the minimum effort required to output Core Graphics (Quartz) text to a window. The window manager and some CG routines (text and shadows) and data types are introduced.   The Apple and File menus (Quit command and keyboard equivalent) are provided "free" with the RunApplicationLoop routine.  The window is non moveable.



Demo2.p


A bit fancier implementation of graphic output to a window and how, multiple windows as well as moving and updating them are automatically handled by the carbon framework with a minimum of setup effort.  The windows are moveable but non resizable.










Demo3.p


Same graphic as Demo 2 but implemented using some rudimentary context coordinate manipulation.  Also, the window name is set using the CF string routine CFSTR.   The windows can be resized but the contained images are not updated completely.











Demo4.p


Fancier CG graphic commands are demonstrated including dashed bezier lines.  Also, in an attempt to allow the user more control over the windows, some fundamental event manager routines are introduced including how to install an event handler callback routine.  These routines are fundamental for all functional carbon applications which do more than the absolute bare minimum.  The windows are resizable and the graphics are redrawn after resizing but still not perfectly.



Demo5.p


Windows are defined using nib files created using Apple’s Interface Builder Application. Randomly generated rectangles are drawn in the windows.  A default menu bar is also defined via the nib file providing the standard applications menus and menu items.  A few of the items actually are functional with very little effort expended in the code.  A few more CG graphics routines are introduced and the output provides a few more examples of how alpha (transparency) works.   Some peculiarities of the the ubiquitous random function are demonstrated.  Again, the windows still are not quite redrawn correctly using the simple window manager routines.



Demo6.p


In this demo, HIViews are introduced which solve the problem of window redrawing.   With the help of Apple's Interface Builder, two windows are created each with an embedded view.  In the code, the same identical event handler is attached to each view. The first window redraws the view after the user has finished resizing the window. The second window continually and dynamically redraws the view contents during the resizing.  The concept of a CG path is introduced with several filled and stroked quadratic curves showing what happens when paths are closed or not closed “correctly.”.  The effect of the default clipping region of the view is illustrated.  Also, the effect of multiple layers of alpha is demonstrated.



Demo7.p


In this demo, two HIViews are installed in one window.  In view 1, various coordinate systems are compared, translated and updated dynamically as the window is resized.  In view 2, the mouse coordinates are updated dynamically as the user moves the pointer over various parts of the screen.   A separate event handler for the mouse is used to communicate the mouse tracking information to view 2’s drawing update handler.  View clipping can be seen very clearly in this demo.






Demo 8.p


Not fully finished commented or debugged yet.  Shows how easy it is (after figuring it out!) to rotate objects using coordinate transforms.













Demo 9.p

Demo 10.p


These are rough programs so far but included in case they are of interest to anyone.   They seem to execute OK and are meant to illustrate more view stuff.   They may not compile as is without other additional unit(s) for utility routines.