Next: 5 Adding a menu Up: GNUstep Renaissance Previous: 3 Writing the gsmarkup

4 Loading the gsmarkup file from the program

We now need to load the Window.gsmarkup file (which we have created in the previous section) in our program. GNUstep Renaissance provides facilities to load gsmarkup files; ``loading'' a gsmarkup file means reading the file and creating all the objects (and connections, as will be clear later) described in the file. In this case, loading the file will create a single, empty, window.

In order to load the Window.gsmarkup file, we just need to use the code

  [NSBundle loadGSMarkupNamed: @"Window"  owner: self];
this will parse the Window.gsmarkup file and create the objects (and connections) contained in the file. We can ignore the owner: argument for now as we don't need it yet; passing self (where self is the application's delegate) is OK for now. This code will look for a file Window.gsmarkup in the main bundle of the application, and load it. To compile this line, you need to include the Renaissance header file <Renaissance/Renaissance.h> at the beginning of your program.

The full program is then:

#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
#include <Renaissance/Renaissance.h>

@interface MyDelegate : NSObject
{}
- (void) applicationDidFinishLaunching: (NSNotification *)not;
@end

@implementation MyDelegate : NSObject 

- (void) applicationDidFinishLaunching: (NSNotification *)not;
{
  [NSBundle loadGSMarkupNamed: @"Window"  owner: self];
}
@end

int main (int argc, const char **argv)
{ 
  [NSApplication sharedApplication];
  [NSApp setDelegate: [MyDelegate new]];

  return NSApplicationMain (argc, argv);
}
Save this code in a main.m file. Please note that, for simplicity in this first example, we have omitted the creation of the application menu; we'll add it in later sections. The given #include directives work on both GNUstep and Apple Mac OS X.

To complete the example, we provide a GNUmakefile for the application:

include $(GNUSTEP_MAKEFILES)/common.make

APP_NAME = Example
Example_OBJC_FILES = main.m
Example_RESOURCE_FILES = Window.gsmarkup

ifeq ($(FOUNDATION_LIB), apple)
  ADDITIONAL_INCLUDE_DIRS += -framework Renaissance
  ADDITIONAL_GUI_LIBS += -framework Renaissance
else
  ADDITIONAL_GUI_LIBS += -lRenaissance
endif

include $(GNUSTEP_MAKEFILES)/application.make
The few lines starting with
ifeq ($(FOUNDATION\_LIB), apple)
add -framework Renaissance on Apple Mac OS X, and -lRenaissance on GNUstep, which makes sure that your program compiles and runs on both GNUstep and Apple Mac OS X.

The program should now compile (using 'make') and run (using ``openapp Example.app'' on GNUstep, and ``open Example.app'' on Apple Mac OS X). The program won't do much, except displaying a small empty window. To close it, you probably need to kill it (from the command line by typing Control-C, or using the window manager).

We are assuming here that you use gnustep-make to compile on Apple Mac OS X; you can do the equivalent with ProjectBuilder if you really want: create a Cocoa application project, then add the Objective-C source file, and the Window.gsmarkup resource file. You also need to specify that you want the program to use the Renaissance framework. Then you should be able to compile and build the program. We will make no more mention of Project Builder; it should be easy to adapt the examples to build using Project Builder if you want, but using gnustep-make and the provided GNUmakefiles will give you almost seamless portability, since the same code will compile without changes on GNUstep and Apple Mac OS X.


Next: 5 Adding a menu Up: GNUstep Renaissance Previous: 3 Writing the gsmarkup
Nicola 2003-01-31