discuss-gnustep
[Top][All Lists]
Advanced

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

bug with addSubview ??


From: Paddy Smith
Subject: bug with addSubview ??
Date: Sat, 10 Feb 2007 11:23:21 +0000

Hi,

The upper textfield appears a long way from where I expect it
(off the window initially, have to resize to see it).

Is this just a bug or am I doing something silly ?

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

@interface MySubView : NSView {
       NSTextField     *p0;
}
@end
@implementation MySubView

- (id) initWithFrame: (NSRect)r
{
       self = [super initWithFrame: r];

       float pad=10.0;
       p0=[[NSTextField alloc] initWithFrame:

NSMakeRect(r.origin.x+pad,r.origin.y+pad,r.size.width-(pad*2.0),r.size.height-(pad*2.0))];
       [p0 setStringValue: @"test"];
       [self addSubview: p0];

       return self;
}

- (void) drawRect: (NSRect)rect
{
       float pad   = 5.0;
       float left  = rect.origin.x+pad;
       float right = rect.origin.x+rect.size.width-pad;
       float bot   = rect.origin.y+pad;
       float top   = rect.origin.y+rect.size.height-pad;

       NSPoint p1 = NSMakePoint(left,bot);
       NSPoint p2 = NSMakePoint(right,bot);
       NSPoint p3 = NSMakePoint(right,top);
       NSPoint p4 = NSMakePoint(left,top);

       NSBezierPath *square = [NSBezierPath bezierPath];
       [square moveToPoint:p1];
       [square lineToPoint:p2];
       [square lineToPoint:p3];
       [square lineToPoint:p4];
       [square lineToPoint:p1];
       [[NSColor blackColor] set];
       [square stroke];
}
@end

@interface MyView : NSView {}
@end
@implementation MyView
- (id) initWithFrame: (NSRect)r
{
       [super initWithFrame: r];

       float offset=r.size.height/2.0;
       MySubView *mySV;

       mySV=[[MySubView alloc] initWithFrame:
               NSMakeRect(r.origin.x,r.origin.y,r.size.width,offset)];
       [mySV setAutoresizingMask:
NSViewWidthSizable|NSViewHeightSizable|NSViewMaxYMargin];
       [self addSubview: mySV];
       RELEASE(mySV);

       mySV=[[MySubView alloc] initWithFrame:
               NSMakeRect(r.origin.x,r.origin.y+offset,r.size.width,offset)];
       [mySV setAutoresizingMask:
NSViewWidthSizable|NSViewHeightSizable|NSViewMinYMargin];
       [self addSubview: mySV];
       RELEASE(mySV);

       return self;
}
@end

@interface AppController : NSObject
{
   NSWindow    *window;
   MyView      *myV;
}
- (void)applicationWillFinishLaunching:(NSNotification *) not;
- (void)applicationDidFinishLaunching:(NSNotification *) not;
@end

@implementation AppController
- (void) applicationWillFinishLaunching: (NSNotification *) not
{
  NSMenu *menu = [NSMenu new];
  [menu addItemWithTitle: @"Quit" action: @selector(terminate:)
keyEquivalent: @"q"];
  [NSApp setMainMenu:menu];
  RELEASE(menu);

  window = [[NSWindow alloc] initWithContentRect: NSMakeRect(200,
200, 100, 100)
                                       styleMask: (NSTitledWindowMask |
                                                   NSMiniaturizableWindowMask |
                                                   NSResizableWindowMask)
                                         backing: NSBackingStoreBuffered
                                           defer: YES];
  [window setTitle: @"Hello World"];

  myV=[[MyView alloc] initWithFrame: [[window contentView] frame]];
  [myV setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
  [[window contentView] addSubview: myV];
}

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

int main(int argc, const char *argv[])
{
  NSAutoreleasePool *pool;
  AppController *delegate;

  pool = [[NSAutoreleasePool alloc] init];
  delegate = [[AppController alloc] init];

  [NSApplication sharedApplication];
  [NSApp setDelegate: delegate];

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

Regards,
Paddy




reply via email to

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