Next: 8 Another small example Up: GNUstep Renaissance Previous: 6 Changing the window

7 Adding a button in the window

We now want to complete implementing the program of the previous tutorial using GNUstep Renaissance: we want to add a button to the window; clicking the button should print Hello! on the console.

Adding the button is just a matter of modifying our Window.gsmarkup file, so that it doesn't create the window empty, but with a button inside it:



    <window title="This is a test window" closable="no">
      <button title="Print Hello!" action="printHello:" target="#NSOwner" />


Because the <button> tag is inside the <window> tag, the button will be created inside the window. The button will be created with title Print Hello!, and when you click on it, the method printHello: of the object ``#NSOwner'' will be called.

The syntax #NSOwner in gsmarkup files is special, and means ``the file owner''. The file owner is the object which is passed as an argument to the loadGSMarkupNamed:owner: method when loading the file, and normally is used to connect together the objects in the interfaces created by a gsmarkup file, to the rest of your application. We have passed the application delegate (an object of a class implemented by us) as the file owner in our examples, which is a reasonably good choice in this situation, since we can add custom methods to it, and call them from the interface.

In this case, by adding target="#NSOwner", we have specified that the target of the button is the file owner object. We can then implement the method printHello: of the file owner object to do something, and then clicking on the button will cause that method (and your specific code) to be executed.

To finish our tutorial example, we just need to add this method printHello: to the file owner. Here is the code after this final change -

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

@interface MyDelegate : NSObject
- (void) printHello: (id)sender;
- (void) applicationDidFinishLaunching: (NSNotification *)not;

@implementation MyDelegate : NSObject 

- (void) printHello: (id)sender
  printf ("Hello!\n");

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

int main (int argc, const char **argv)
  MyDelegate *delegate;
  [NSApplication sharedApplication];

  delegate = [MyDelegate new];
  [NSApp setDelegate: delegate];

#ifdef GNUSTEP
  [NSBundle loadGSMarkupNamed: @"Menu-GNUstep"  owner: delegate];
  [NSBundle loadGSMarkupNamed: @"Menu-OSX"  owner: delegate];

  RELEASE (pool);
  return NSApplicationMain (argc, argv);

Finally, we want to underline that the most unpleasant part of the work has been silently done by GNUstep Renaissance for us. If you check the original code in the previous tutorial, you will see that we have had to compute the button size, and to use the button size to build up the window size. This has now all silently been done by GNUstep Renaissance for us. GNUstep Renaissance has made the button of the right size to display its title, then it has sized the window to fit the only object it contains - the button. In more complex windows, the help GNUstep Renaissance gives us by automatically sizing and laying objects and windows can considerably reduce development time.

Next: 8 Another small example Up: GNUstep Renaissance Previous: 6 Changing the window