Programming under GNUstep—An Introduction

With GNUstep, you can develop a new application or port one from Mac OS X.

This article provides an introduction to GNUstep development. It guides the reader through installing GNUstep and developing a small application. The Objective-C language, a true object-oriented superset of ANSI C, is used in this article. Its syntax is simple, unambiguous and easy to learn.

A Short History

OpenStep, which was proposed as an open standard by NeXT Computer, Inc., in 1994, is a collection of advanced object-oriented APIs designed for rapidly developing applications in the Objective-C language. It was designed to be implemented independently of the computer's operating system. To this end, there were implementations of the standard for Mach, Microsoft Windows (NT and 95), Sun Solaris and HP-UX.

In the early 1990s, GNUstep, a free implementation of the OpenStep standard, was born. GNUstep aims to be a fully compliant OpenStep implementation, supporting a wide range of operating systems, while being entirely free. GNUstep is released under the terms of the LGPL license. GNUstep currently works well on Linux, Solaris and most BSDs, and there is also preliminary support for Microsoft Windows.

As was widely reported at that time, Apple bought NeXT in 1996. Now, with Mac OS X, Apple is moving forward with Cocoa, an extension of the OpenStep API. While creating Cocoa, Apple added new classes that enrich the API. GNUstep is partially supporting those new features, and better support is being added every day.

GNUstep also offers a well-defined separation between the user-interface classes (part of the application kit) and the underlying windowing system. This layer, called gnustep-back, provides implementations for various windowing systems, allowing gnustep-gui to work properly under various back ends. Back ends currently are being developed for the X Window System, Microsoft Windows and Ghostscript. Figure 1 shows the different layers of GNUstep.

Figure 1. The Different Layers of GNUstep

GNUstep also offers bindings to other languages, like Java (using JIGS) and Ruby (using RIGS), allowing developers to create applications using those languages. Additionally, GNUstep offers powerful scripting capabilities through StepTalk, which allows you to create scriptable applications or servers using Smalltalk. Finally, GNUstep offers an extension so that Guile, a version of Scheme, can be used as a scripting language.

Installing GNUstep

In order to compile GNUstep under Linux, that the following programs and libraries (with headers) need to be installed (versions indicated or higher): GNU make 3.75, GNU libc 2.1.2, GNU gcc 3.0.3, XFree86 4.1, ffcall 1.8, OpenSSL 0.9.6c (not required but recommended), libtiff 3.5.5 and libxml 2.2.3.

Once all those requirements are satisfied, you are now ready to download and compile GNUstep. We are going to install an unstable release of GNUstep because new features have been added recently that are worth using and mentioning, especially regarding Gorm—the Graphical Object Relationship Modeler. Gorm is a clone of OpenStep's excellent interface-builder application. Gorm allows the developer to create user interfaces quickly from a palette of standard objects.

Get the following releases from the GNUstep web site: GNUstep make 1.5.1, GNUstep base 1.5.1, GNUstep gui 0.8.3, GNUstep back 0.8.3 and Gorm 0.2.0.

Once you have downloaded those releases, you are ready to install GNUstep. You must first install GNUstep make, base, gui and finally, back. Look at the INSTALL file of each release carefully for installation instructions. For detailed instructions on installing GNUstep, refer to the GNUstep Build Guide for Unix Systems (see Resources).

Once GNUstep is compiled and installed, load the proper set of environment variables by executing a shell script (adjust the path, if necessary).

Bash users would type:

# . /usr/GNUstep/System/Makefiles/GNUstep.sh

And, C-shell users would type:

# . /usr/GNUstep/System/Makefiles/GNUstep.csh
To compile and install Gorm, simply uncompress the file and type (as root):
# cd Gorm
# make
# make install

Developing a Small Application

Now that GNUstep and Gorm are compiled and installed, you can develop your small application, a simple TIFF image viewer. This application will use the Model-View-Controller (MVC) design. The MVC design pattern separates a software component into three pieces: a model, a view and a controller. Figure 2 shows how the MVC pattern is used in our small application.

Figure 2. Using the MVC Pattern

Under GNUstep, use Gorm to create the views of an application. This application allows you to create the user interface of your application. Once created, the user interface will be saved as an archive file (containing a serialized object graph) in its own subdirectory. To start Gorm, simply type:

# openapp Gorm.app

Once Gorm has started, show the Inspector and the Palettes windows by clicking on the Inspector... and Palettes... menu items from the Tools menu. Now, create a new application by clicking on the New Application menu item from the Document menu. Once those steps are completed, your desktop should look like Figure 3.

Figure 3. Creating a New Application

Let's examine Figure 3 in order to explain the different elements. Table 1 shows the different elements, and their roles.

Table 1. Parts of the Gorm Interface

From the Palettes window, drag an NSImageView object to the empty window (My Window) and drop it. Figure 4 shows the icon that represents the NSImageView object.

Figure 4. NSImageView Object

Now, click on the NSImageView in the window, and the inspector window will update itself so you can set the correct attributes to this object. Set the border to Line Border and the scaling to To Fit. The Inspector window for the attributes of the NSImageView should look like Figure 5.

Figure 5. The Inspector Window for the Attributes of NSImageView

Now, from the Inspector window, select the Size item from the combo box at the top to set the auto-sizing properties of the NSImageView object. Setting the auto-sizing properties to NSImageView will allow the object to resize properly if the user resizes the window. Set the parameters by clicking on the lines inside the Auto-sizing box to look like those in Figure 6.

Figure 6. Setting the Auto-Sizing Parameters

Next set the window attributes. Click on My Window in the File window. Set the title to Tiff Viewer in the Inspector window. The Inspector window for the attributes should look like Figure 7.

Figure 7. Setting the Window Attributes

You need to add a menu item so you can open an image file. To create a new menu item, choose the menu palette from the Palettes window, drag the Item menu to the Main Menu (above the Hide menu item) and drop it. Click on the newly created Item menu option in the Main Menu, set its title from the Inspector window to Load Image and press Return.

Now, you need to create a controller class that will interact with the user interface. Click on the Classes icon from the File window and select the NSObject item from the tree view. Click on the Classes menu item in the Gorm menu, and select Create Subclass.... In the Classes tree view in the File window, you will now find a NewClass entry. Double-click on the NewClass item and replace the text with “AppController”.

Next, you need to create an outlet for your NSImageView object. An outlet is an instance variable that refers to an object—in this particular case, the NSImageView object. To add an outlet to your AppController class, click on the Outlet icon to the right of the AppController class (in the File window); click on the Classes menu item in the Gorm menu, and select Add Outlet/Action.... In the Classes tree view in the File window, you will now find a newOutlet entry as a child item of AppController. Double-click on the newOutlet item, and replace the text with “imageView”. Repeat these steps to add a window outlet.

After creating the outlet, you need to add an action to your controller. An action is a target-action method. To add one to the controller, click on the Action icon to the right of the AppController class (in the File window); click on the Classes menu item in the Gorm menu, and select Add Outlet/Action.... In the Classes tree view in the File window, you will now find a newAction: entry as a child item of AppController. Double-click on the newAction: item, and replace the text with “loadImage:”.

You are now ready to instantiate your AppController class in order to produce a particular object from its class template. You need to instantiate your controller class because Gorm connects live objects. Select the AppController item in the File window's tree view, and click on the Instantiate menu item from the Classes menu.

Now you can now connect your live objects (in this case, NSImageView) to your outlets and set the action. To connect the NSImageView object to your AppController instance, click the Objects icon in the File window, then, while pressing the Ctrl key, click and drag from the AppController icon (from the File window) to the NSImageView object in the Tiff Viewer window. Click on the imageView item in the Inspector window, and click on the button Connect. This connects the NSImageView live object to the imageView outlet. To connect the window outlet, while pressing the Ctrl key, click and drag the AppController icon (from the File window) to the My Window icon (from the File window). Now, click on the window item from the Inspector window, and finally, click on the Connect button. Figure 8 illustrates the connection being made between the window object and its outlet.

Figure 8. The Connection Being Made between the Window Object and Its Outlet

To set the action of the Load Image menu item, Control-click and drag on the menu item to the AppController instance icon in the File window. Then, click on target in the Inspector window, select the loadImage: item, and click on the Connect button. This sets the action of the Load Image menu item to loadImage:. So, when the Load Image menu item receives a click from the user, the loadImage: method will be invoked.

Now, save everything by choosing the Save menu item from the Document menu. Give it the name MainMenu. This creates a MainMenu.gorm directory, holding the archived view of the application.

Finally, create the AppController class' source files by selecting the AppController class entry in the Classes tree view and choose the Create Class's Files... from the Classes menu. Leave the names as they are and simply click on the OK button. This creates two files: AppController.m and AppController.h.

Now, quit Gorm, open your favorite editor and modify AppController.h so it looks like Listing 1. The complete source code with comments is available; see Resources.

Listing 1. AppController.h

Then, modify AppController.m (the implementation of the AppController class) so that it looks like Listing 2.

Listing 2. AppController.m

Now, create a very basic Model for the application—ImageModel. This small class will hold the current displayed image. Now create the implementation of the ImageModel class as shown in Listings 3 and 4.

Listing 3. ImageModel.h

Listing 4. ImageModel.m

Now, create a small property list, shown in Listing 5, in which you specify the application name, description and the name of the main Gorm file to load upon startup.

Listing 5. TiffViewerInfo.plist

Then, create a small GNUmakefile in order to compile and link your small application. The GNUmakefile should look like Listing 6.

Listing 6. GNUmakefile

Finally, compile and start the small application:

# make
# openapp TiffViewer.app

Once the application starts, click on the Load Image menu item and select a TIFF file. It should display properly in the window, as shown in Figure 9.

Figure 9. Displaying an Image

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

GNUstep.sh location

Greg in St. Peters's picture

Took me a while to figure out where GNUstep.sh is located for Ubuntu. In fact, it seems some people get so frustrated with this that they uninstall all their gnustep packages and build it from source. However, I finally found this file in /usr/share/GNUstep/Makefiles.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState