discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSGraphicsContext


From: Honor Harrington
Subject: NSGraphicsContext
Date: Sat, 22 Jan 2005 15:27:31 -1000
User-agent: KMail/1.7.2

Hi -

I'm a total newbie - and want to learn GNUstep. I have played
with the standard gui objects and have been sucessful. Now I
want to do some graphics; so far, no joy. I have been trying to
use NSGraphicsContext, but can get nothing to display. I must
be missing the point somewhere.

Anyway, here is the stripped-down program that attempts to draw
a single horizontal line across the window. The make runs w/o
errors or warnings and the window displays, but no line is shown.

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

@interface MyDrawing : NSView
{
}
- (void) drawRect: (NSRect)rect;
@end

@implementation MyDrawing : NSView
- (void) drawRect: (NSRect)rect
{
  NSRect brect;
  brect = [self bounds];
  NSGraphicsContext *ctxt = GSCurrentContext();

  // do I need this?
  [ctxt DPSinitgraphics];
  // ---------------

  [ctxt DPSsetlinewidth: 2];

  // just trying to paint a line...
  [ctxt DPSmoveto:
          brect.origin.x+10:
          brect.origin.y+(brect.size.height/2)];
  [ctxt DPSlineto:
          brect.origin.x+brect.size.width-10:
          brect.origin.y+(brect.size.height/2)];

  // and nothing shows up in the window...
  // if I NSLog the bounds rect - it's good, line width is set to 2
}
@end

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

@implementation MyDelegate : NSObject 
- (void) dealloc
{
  RELEASE (myWindow);
}

- (void) createWindow
{
  NSRect rect;
  MyDrawing *myDrawing;
  unsigned int styleMask = 
      NSTitledWindowMask 
    | NSMiniaturizableWindowMask
    | NSResizableWindowMask;

  myDrawing = AUTORELEASE ([MyDrawing new]);

  rect = NSMakeRect (100, 100, 100, 100 );

  myWindow = [NSWindow alloc];
  myWindow = [myWindow initWithContentRect: rect
                       styleMask: styleMask
                       backing: NSBackingStoreBuffered
                       defer: NO];
  [myWindow setTitle: @"This is a test window"];
  [myWindow setContentView: myDrawing];
}

- (void) applicationWillFinishLaunching: (NSNotification *)not
{
  [self createWindow];
}

- (void) applicationDidFinishLaunching: (NSNotification *)not;
{
  [myWindow makeKeyAndOrderFront: nil];
}
@end

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

  return NSApplicationMain( argc, argv );
}

Can someone help - or - point me to some graphics code I can
study?

-- 
"Let's be about it" - Honor




reply via email to

[Prev in Thread] Current Thread [Next in Thread]