/* 
   buttons.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
{
	id textField;
	id textField1;
}

@end

@implementation Controller

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

- (void) buttonAction:(id)sender
{
	NSLog (@"buttonAction:");
}

- (void) buttonAction2:(id)sender
{
	NSLog (@"buttonAction2:");
	[textField setStringValue:[sender intValue] ? @"on" : @"off"];
}

- (void) setTextField:(id)anObject
{
	ASSIGN(textField, anObject);
}

- (void) buttonPressed:sender
{
	NSLog (@"textfield value = %@", [textField1 stringValue]);
	[textField1 setStringValue: @"Hello"];
}

- (void) setTextField1:object
{
	textField1 = object;
}

- (void) buttonSwitchView:(id)sender
{
	NSLog (@"selected value = %@", [sender titleOfSelectedItem]);
}

- (void) comboBoxButton:(id)sender
{
	NSLog (@"selected value = %@", [sender objectValueOfSelectedItem]);
}

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSWindow *win;
NSRect wf = {{100, 100}, {400, 400}};
NSView *v;
NSView* anotherView1;
NSView* anotherView2;
NSButton *mpush, *ponpoff, *toggle, *swtch, *radio, *mchange, *onoff, *light;
NSButton *ponpoff1;
NSRect bf0 = {{5, 5}, {200, 50}};
NSRect bf1 = {{5, 65}, {90, 45}};
NSRect bf10 = {{100, 65}, {90, 45}};
NSRect bf2 = {{5, 115}, {90, 26}};
NSRect bf3 = {{5, 170}, {200, 26}};
NSRect bf4 = {{5, 225}, {200, 26}};
NSRect bf5 = {{5, 280}, {200, 50}};
NSRect bf6 = {{5, 10}, {100, 20}};
NSRect bf7 = {{120, 115}, {90, 26}};
NSRect anotherView1Frame = {{1, 335}, {200, 70}};
NSRect anotherView2Frame = {{2, 10}, {180, 50}};
NSRect textFieldRect = {{125, 10}, {60, 20}};
NSTextField* txtField;
id button;
NSTextField* tfield;
NSRect textRect = { {260, 100}, {100, 20} };
NSRect buttonRect = { { 300, 130 }, { 53, 20 } };
NSColorWell *colorWell;
NSRect cwf = {{300, 30}, {40, 40}};
NSRect frame = {{250, 200}, {100, 100}};
NSForm* form;
NSFormCell *c;
NSPopUpButton *pushb;
NSComboBox *combo;

    NSLog(@"Starting the application\n");

	win = [[NSWindow alloc] initWithContentRect:wf
							styleMask:GSAllWindowMask
							backing:NSBackingStoreBuffered	
							defer:NO];

	[win setTitle:@"mySTEP Buttons"];

    v = [win contentView];

    NSLog(@"Create the buttons\n");
    mpush = [[NSButton alloc] initWithFrame: bf0];
    [mpush setTitle: @"MomentaryPush"];
    [mpush setTarget:self];
    [mpush setAction:@selector(buttonAction:)];
    [mpush setContinuous:YES];

    ponpoff = [[NSButton alloc] initWithFrame: bf1];
    [ponpoff setButtonType:NSToggleButton];
    [ponpoff setTitle: @"Toggle"];
    [ponpoff setAlternateTitle: @"Alternate"];
    [ponpoff setImage:[NSImage imageNamed:@"NSSwitch"]];
    [ponpoff setAlternateImage:[NSImage imageNamed:@"NSHighlightedSwitch"]];
    [ponpoff setImagePosition:NSImageAbove];
    [ponpoff setAlignment:NSCenterTextAlignment];

    ponpoff1 = [[NSButton alloc] initWithFrame: bf10];
    [ponpoff1 setButtonType:NSToggleButton];
    [ponpoff1 setTitle: @"Toggle"];
    [ponpoff1 setAlternateTitle: @"Alternate"];
    [ponpoff1 setImage:[NSImage imageNamed:@"NSSwitch"]];
    [ponpoff1 setAlternateImage:[NSImage imageNamed:@"NSHighlightedSwitch"]];
    [ponpoff1 setImagePosition:NSImageBelow];
    [ponpoff1 setAlignment:NSCenterTextAlignment];

    toggle = [[NSButton alloc] initWithFrame: bf2];
    [toggle setButtonType: NSToggleButton];
    [toggle setTitle: @"Toggle"];

    light = [[NSButton alloc] initWithFrame: bf7];
    [light setButtonType: NSMomentaryLight];
    [light setTitle: @"Light"];

    swtch = [[NSButton alloc] initWithFrame: bf3];
    [swtch setButtonType: NSSwitchButton];
    [swtch setBordered: NO];
    [swtch setTitle: @"Switch"];
    [swtch setAlternateTitle: @"Alternate"];

    radio = [[NSButton alloc] initWithFrame: bf4];
    [radio setButtonType: NSRadioButton];
    [radio setBordered: NO];
    [radio setTitle: @"Radio"];
    [radio setAlternateTitle: @"Alternate"];

    mchange = [[NSButton alloc] initWithFrame: bf5];
    [mchange setButtonType: NSMomentaryChangeButton];
    [mchange setTitle: @"MomentaryChange"];
    [mchange setAlternateTitle: @"Alternate"];

    anotherView1 = [[NSView alloc] initWithFrame:anotherView1Frame];
    anotherView2 = [[NSView alloc] initWithFrame:anotherView2Frame];

    onoff = [[NSButton alloc] initWithFrame: bf6];
    [onoff setButtonType: NSOnOffButton];
    [onoff setTitle: @"OnOff"];
    [onoff setTarget:self];
    [onoff setAction:@selector(buttonAction2:)];

    txtField = [[[NSTextField alloc] initWithFrame:textFieldRect] autorelease];
    [self setTextField:txtField];
    [txtField setStringValue:@"off"];

	colorWell = [[NSColorWell alloc] initWithFrame: cwf];
	[colorWell setColor:[NSColor greenColor]];

    tfield = [[NSTextField alloc] initWithFrame:textRect];
    [tfield setStringValue:@"abcdefghijklmnopqrstuvwxyz"];
    [tfield setAlignment:NSCenterTextAlignment];
    [tfield setNextText:txtField];
    [v addSubview:tfield];
	[win setInitialFirstResponder:tfield];

    [self setTextField1:tfield];
    [txtField setNextText:mchange];
    [mchange setNextKeyView:tfield];

	textRect.origin.y -= 25;
//  tfield = [[NSTextField alloc] initWithFrame:textRect];
    tfield = [[NSSecureTextField alloc] initWithFrame:textRect];
    [tfield setStringValue:@"abcdefghijklmnopqrstuvwxyz"];
    [tfield setAlignment:NSCenterTextAlignment];
    [[tfield cell] setScrollable:NO];
    [v addSubview:tfield];

    button = [[NSButton alloc] initWithFrame:buttonRect];
    [button setButtonType:NSMomentaryPushButton];
    [v addSubview:button];
    [button setTarget:self];
    [button setAction:@selector(buttonPressed:)];

	form = [[NSForm alloc] initWithFrame:frame];
	c = [form addEntry:@"Field1"];
	[c setEditable:YES];
	[c setStringValue:@"Test"];
	[form addEntry:@"Field2"];
	[form addEntry:@"Field3"];
	[form sizeToCells];
	[v addSubview:form];

	pushb = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(200,375,80,20) 
								   pullsDown:YES];
	[pushb addItemWithTitle:@"Devices"];
	[pushb addItemWithTitle:@"Network"];
	[pushb addItemWithTitle:@"Printers"];
	[pushb addItemWithTitle:@"Austin"];
	[pushb addItemWithTitle:@"Powers"];
	[pushb addItemWithTitle:@"Shag"];
	[pushb setTarget:self];
	[pushb setAction:@selector(buttonSwitchView:)];
	[v addSubview:pushb];

	pushb = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(300,375,80,20)];
	[pushb addItemWithTitle:@"Devices"];
	[pushb addItemWithTitle:@"Network"];
	[pushb addItemWithTitle:@"Printers"];
	[pushb addItemWithTitle:@"Austin"];
	[pushb addItemWithTitle:@"Powers"];
	[pushb addItemWithTitle:@"Shag"];
	[pushb setTarget:self];
	[pushb setAction:@selector(buttonSwitchView:)];
	[v addSubview:pushb];

	combo = [[NSComboBox alloc] initWithFrame:NSMakeRect(300,330,80,22)];
	[combo setTarget:self];
	[combo setAction:@selector(comboBoxButton:)];
	[combo addItemWithObjectValue: @"lady"];
	[combo addItemWithObjectValue: @"3Jane"];
	[combo addItemWithObjectValue: @"Turing"];
	[v addSubview:combo];

    NSLog(@"Make the buttons subviews\n");
    [v addSubview: mpush];
    [v addSubview: ponpoff];
    [v addSubview: ponpoff1];
    [v addSubview: toggle];
    [v addSubview: swtch];
    [v addSubview: radio];
    [v addSubview: mchange];
    [v addSubview:anotherView1];
    [anotherView1 addSubview:anotherView2];
    [anotherView2 addSubview:onoff];
    [anotherView1 addSubview:txtField];
    [v addSubview:light];
	[v addSubview: colorWell];

    [v display];
    [win orderFront:nil];
}

@end

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