/*
   scrollview2.m

   This file is part of the mySTEP Library and is provided
   under the terms of the GNU Library General Public License.
*/

#import <AppKit/AppKit.h>


@interface Controller : NSObject
{
	NSMatrix *matrix;
}

@end

@implementation Controller

- (id) init
{
	[[NSApplication sharedApplication] setDelegate: self];	
}

- (void) setButtonTitles
{
int i, j, index = 0;
int numRows = [matrix numberOfRows];
int numCols = [matrix numberOfColumns];
id cell;

  for (i = 0; i < numRows; i++)
    for (j = 0; j < numCols; j++) {
      cell = [matrix cellAtRow:i column:j];
      [cell setTag:index];
      [cell setTitle:[NSString stringWithFormat:@"button %d, %d (%d)", i, j, index]];
      [cell setTarget:self];
      [cell setAction:@selector(handleCellAction:)];
      index++;
    }
}

- (void) handleCellAction:sender
{
	NSLog (@"handleCellAction: sender = %@", [[sender selectedCell] title]);
}

- (void) addRow:sender
{
	[matrix addRow];
	[self setButtonTitles];
	[matrix sizeToCells];
	[matrix setNeedsDisplay:YES];
}

- (void) addColumn:sender
{
	[matrix addColumn];
	[self setButtonTitles];
	[matrix sizeToCells];
	[matrix setNeedsDisplay:YES];
}

- (void) removeRow:sender
{
	if ([matrix selectedRow] >= 0) 
		{
		[matrix setNeedsDisplay:YES];
		[matrix removeRow:[matrix selectedRow]];
		[matrix sizeToCells];
		}
}

- (void) removeColumn:sender
{
	if ([matrix selectedColumn] >= 0) 
		{
		[matrix removeColumn:[matrix selectedColumn]];
		[matrix setNeedsDisplay:YES];
		[matrix sizeToCells];
		}
}

- (void) setMatrixMode:sender
{
	NSLog (@"setMatrixMode: %d", [[sender selectedCell] tag]);
	[matrix setMode:[[sender selectedCell] tag]];
}

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSWindow *window = [[NSWindow alloc] init];
NSScrollView *scrollView;
NSMatrix *selectionMatrix;
NSButtonCell *buttonCell;
NSButton *addRowButton, *removeRowButton, *addColButton, *removeColButton;
NSRect matrixRect = NSZeroRect;
NSRect scrollViewRect = {{20, 115}, {350, 235}};
NSRect winRect = {{100, 100}, {400, 450}};
NSRect selectionMatrixRect = {{30, 15}, {85, 95}};
NSRect addRowRect = {{160, 70}, {95, 24}};
NSRect removeRowRect = {{160, 32}, {95, 24}};
NSRect addColRect = {{272, 70}, {95, 24}};
NSRect removeColRect = {{272, 32}, {95, 24}};

	buttonCell = [[NSButtonCell new] autorelease];
	[buttonCell setButtonType:NSPushOnPushOffButton];
	matrix = [[[NSMatrix alloc] initWithFrame:matrixRect	// configure matrix
								mode:NSRadioModeMatrix
								prototype:buttonCell
								numberOfRows:0
								numberOfColumns:0]
								autorelease];
	[matrix retain];
	
	scrollView = [[NSScrollView alloc] initWithFrame:scrollViewRect];
	[scrollView setHasHorizontalScroller:YES];
	[scrollView setHasVerticalScroller:YES];
	[scrollView setDocumentView:matrix];
	[[window contentView] addSubview:scrollView];
	
	/* Setup the matrix for different selection types */
	buttonCell = [[NSButtonCell new] autorelease];
	[buttonCell setButtonType:NSRadioButton];
	[buttonCell setBordered:NO];
	[buttonCell setImagePosition:NSImageLeft];			// for NS compatibility
	
	selectionMatrix = [[[NSMatrix alloc] initWithFrame:selectionMatrixRect
										mode:NSRadioModeMatrix
										prototype:buttonCell
										numberOfRows:4
										numberOfColumns:1]
										autorelease];
	[selectionMatrix setTarget:self];
	[selectionMatrix setAutosizesCells:YES];			// for NS compatibility
	[selectionMatrix setAction:@selector(setMatrixMode:)];
	
	buttonCell = [selectionMatrix cellAtRow:0 column:0];
	[buttonCell setTitle:@"Radio"];
	[buttonCell setTag:NSRadioModeMatrix];
	
	buttonCell = [selectionMatrix cellAtRow:1 column:0];
	[buttonCell setTitle:@"Highlight"];
	[buttonCell setTag:NSHighlightModeMatrix];
	
	buttonCell = [selectionMatrix cellAtRow:2 column:0];
	[buttonCell setTitle:@"List"];
	[buttonCell setTag:NSListModeMatrix];
	
	buttonCell = [selectionMatrix cellAtRow:3 column:0];
	[buttonCell setTitle:@"Track"];
	[buttonCell setTag:NSTrackModeMatrix];
	
	[[window contentView] addSubview:selectionMatrix];
	
	addRowButton = [[NSButton alloc] initWithFrame:addRowRect];
	[addRowButton setTitle:@"Add row"];
	[addRowButton setTarget:self];
	[addRowButton setAction:@selector(addRow:)];
	[[window contentView] addSubview:addRowButton];
	
	removeRowButton = [[NSButton alloc] initWithFrame:removeRowRect];
	[removeRowButton setTitle:@"Remove row"];
	[removeRowButton setTarget:self];
	[removeRowButton setAction:@selector(removeRow:)];
	[[window contentView] addSubview:removeRowButton];
	
	addColButton = [[NSButton alloc] initWithFrame:addColRect];
	[addColButton setTitle:@"Add column"];
	[addColButton setTarget:self];
	[addColButton setAction:@selector(addColumn:)];
	[[window contentView] addSubview:addColButton];
	
	removeColButton = [[NSButton alloc] initWithFrame:removeColRect];
	[removeColButton setTitle:@"Remove column"];
	[removeColButton setTarget:self];
	[removeColButton setAction:@selector(removeColumn:)];
	[[window contentView] addSubview:removeColButton];
	
	[window setFrame:winRect display:YES];
	[window orderFront:nil];
}

@end

int
main(int argc, char **argv, char** env)
{
    return NSApplicationMain(argc, argv);
}
