[futurebasic] Positioning subviews within NSTabView

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2016 : Group Archive : Group : All Groups

From: Bernie <bernie.fblist@...>
Date: Thu, 24 Nov 2016 15:29:06 +0000
In the following example, the first tab item has been given a custom view with its rect x value set to 40 pixels. A button, embedded in the custom view, has an x value of 11 pixels. One would expect the button to be inset from the left of the tab view by 40 + 11 = 51 pixels.

Not so. The tab view automatically sets the custom view’s rect to the tab view’s content rect, positioning the button at 10 + 11 = 21 pixels from the left side of the tab view.

I think it’s probably safer (and clearer) to init custom views like so:
     NSView *customView = [[NSView alloc] initWithFrame:[tabView contentRect]];

// ---------------
@interface AppDelegate : NSObject <NSApplicationDelegate>

@implementation AppDelegate

- (void)buildMainMenu {
     NSMenu *mainMenu = [[[NSMenu alloc] initWithTitle:@"MainMenu"] autorelease];
     NSMenuItem *item = [mainMenu addItemWithTitle:@"" action:nil keyEquivalent:@""];
     NSMenu *mApp = [[[NSMenu alloc] initWithTitle:@""] autorelease];
     [mainMenu setSubmenu:mApp forItem:item];
     [mApp addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
     [NSApp setMainMenu:mainMenu];

- (void)buildWindow {
     // window
     NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(131,158,480,360)
          styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
          backing:NSBackingStoreBuffered defer:NO];
     [window setTitle:@"TabView"];
     [window center];
     NSView *contentView = [window contentView];

     // tabView
     NSTabView *tabView = [[NSTabView alloc] initWithFrame:NSMakeRect(13,2,454,344)];
     [tabView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
     [contentView addSubview:tabView];
     [tabView release];

     // tabView items
     NSTabViewItem *item1 = [[NSTabViewItem alloc] initWithIdentifier:@"1"];
     [item1 setLabel:@"Tab 1"];
     [tabView addTabViewItem:item1];
     [item1 release];

     NSTabViewItem *item2 = [[NSTabViewItem alloc] initWithIdentifier:@"2"];
     [item2 setLabel:@"Tab 2"];
     [[item2 view] setFrame:[tabView contentRect]];
     [tabView addTabViewItem:item2];
     [item2 release];

     // custom view
     NSView *view1 = [[NSView alloc] initWithFrame:NSMakeRect(40,33,354,298)];
     [item1 setView:view1];
     [view1 release];

     // button
     NSButton *btn = [[NSButton alloc] initWithFrame:NSMakeRect(11,267,92,32)];
     [btn setBezelStyle:NSRoundedBezelStyle];
     [btn setTitle:@"Button 1"];
     [btn setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
     [view1 addSubview:btn];
     [btn release];

     [window makeKeyAndOrderFront:nil];

#pragma mark - application delegate methods
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
     [self buildMainMenu];
     [self buildWindow];

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application {
     return YES;

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSApplication *app = [NSApplication sharedApplication];
AppDelegate *myDelegate = [[[AppDelegate alloc] init] autorelease];
[app setDelegate:myDelegate];
[app run];
[pool drain];