Skip to content

Instantly share code, notes, and snippets.

@angerman
Created March 2, 2009 14:15
Show Gist options
  • Save angerman/72755 to your computer and use it in GitHub Desktop.
Save angerman/72755 to your computer and use it in GitHub Desktop.
/*
* AppController.j
*
* Created by __Me__ on __Date__.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*/
@import <Foundation/CPObject.j>
@import "Checkbox.j"
var starEmpty, starSet, starActive;
@implementation StarRater : CPView
{
int _value;
int _hoverStars;
CPView _starFrame, _hoverFrame;
id _delegate;
}
+ (void)initialize
{
starEmpty = [[CPImage alloc] initWithContentsOfFile: "Resources/StarRater/empty.gif" size: CPSizeMake(25, 25)];
starSet = [[CPImage alloc] initWithContentsOfFile: "Resources/StarRater/set.gif" size: CPSizeMake(25, 25)];
starActive = [[CPImage alloc] initWithContentsOfFile: "Resources/StarRater/active.gif" size: CPSizeMake(25, 25)];
}
- (id)initWithFrame:(CPRect)frame
{
self = [super initWithFrame: frame];
//debugger;
_starFrame = [[CPView alloc] initWithFrame:[self bounds]];
[_starFrame setBackgroundColor:[CPColor colorWithPatternImage:starSet]];
_hoverFrame = [[CPView alloc] initWithFrame:[self bounds]];
[_hoverFrame setBackgroundColor:[CPColor colorWithPatternImage:starActive]];
_hoverStars = 0;
[self setBackgroundColor:[CPColor colorWithPatternImage:starEmpty]];
[self _redraw];
[self _hover];
[self addSubview: _starFrame];
[self addSubview: _hoverFrame];
return self;
}
- (void)setValue:(int)value
{
_value = value;
}
- (int)intValue
{
return _value;
}
- (void)setDelegate:(id)aDelegate
{
if(_delegate == aDelegate)
return;
_delegate = aDelegate;
}
- (id)delegate
{
return _delegate;
}
- (void)_redraw
{
[_starFrame setFrameSize:CPSizeMake(_value * 25, 25)];
}
- (void)_hover
{
[_hoverFrame setFrameSize:CPSizeMake(_hoverStars * 25, 25)];
}
- (void)mouseDown:(CPEvent)anEvent
{
var px_diff = [anEvent locationInWindow].x - CGRectGetMinX([self frame]);
if(px_diff < 5) [self setValue:0];
else [self setValue:Math.ceil(px_diff/25)];
_hoverStars = 0;
[self _redraw];
[self _hover];
}
- (void)mouseExited:(CPEvent)anEvent
{
_hoverStars = 0;
[self _hover];
}
- (void)mouseEntered:(CPEvent)anEvent
{
_hoverStars = 0;
[self _hover];
}
- (void)mouseMoved:(CPEvent)anEvent
{
var px_diff = [anEvent locationInWindow].x - CGRectGetMinX([self frame]);
if(px_diff < 5) starValue = 0;
else starValue = Math.ceil(px_diff/25);
if(starValue == _hoverStars) return;
_hoverStars = starValue;
[self _hover];
}
- (void)controlTextDidChange:(id)aTextField
{
console.log("foo");
console.log([[aTextField stringValue] intValue]);
_value = [[aTextField stringValue] intValue];
[self _redraw];
}
@end
@implementation AppController : CPObject
{
CPTextField user;
CPTextField password;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var field_length = @"______________________";
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
// user field
var userLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[userLabel setStringValue:@"Benutzer"];
[userLabel sizeToFit];
[userLabel setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
user = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[user setStringValue:field_length];
[user sizeToFit];
[user setStringValue:@""];
[user setBordered:YES];
[user setEditable:YES];
[user setBezeled:YES];
[user setBezelStyle:CPTextFieldSquareBezel];
[user setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
// password field
var passwordLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[passwordLabel setStringValue:@"Kennwort"];
[passwordLabel sizeToFit];
[passwordLabel setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
password = [[CPSecureTextField alloc] initWithFrame:CGRectMakeZero()];
[password setStringValue:field_length];
[password sizeToFit];
[password setStringValue:@""];
[password setBordered:YES];
[password setEditable:YES];
[password setBezeled:YES];
[password setBezelStyle:CPTextFieldSquareBezel];
[password setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
// Laying out the fields
// wow, this function is not build in...
function max(a,b) { return a > b ? a : b; }
var Luser_w = CGRectGetWidth([userLabel frame]);
var user_w = CGRectGetWidth([user frame]);
var Lpass_w = CGRectGetWidth([passwordLabel frame]);
var pass_w = CGRectGetWidth([password frame]);
var base_w = max(Luser_w,Lpass_w);
[userLabel setFrameOrigin:CGPointMake(((CGRectGetWidth([contentView bounds]) - base_w - user_w - 10)/2
+ base_w - Luser_w),
(CGRectGetHeight([contentView bounds])/2 - CGRectGetHeight([userLabel frame])))];
[user setFrameOrigin:CGPointMake(((CGRectGetWidth([contentView bounds]) - base_w - user_w - 10)/2
+ base_w + 5),
(CGRectGetHeight([contentView bounds])/2 - CGRectGetHeight([user frame])))];
[passwordLabel setFrameOrigin:CGPointMake(((CGRectGetWidth([contentView bounds]) - base_w - user_w - 10)/2
+ base_w - Lpass_w),
(CGRectGetHeight([contentView bounds])/2 + 10))];
[password setFrameOrigin:CGPointMake(((CGRectGetWidth([contentView bounds]) - base_w - user_w - 10)/2
+ base_w + 5),
(CGRectGetHeight([contentView bounds])/2 + 10))];
[contentView addSubview:userLabel];
[contentView addSubview:user];
[contentView addSubview:passwordLabel];
[contentView addSubview:password];
// add the login button
var button = [[CPButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX([password frame]) - 80.0,
CGRectGetMaxY([password frame]) + 10,
80, 18)];
[button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[button setTarget:self];
[button setAction:@selector(login:)];
[button setTitle:"Anmelden"];
[contentView addSubview:button];
var progress = [[CPProgressIndicator alloc] initWithFrame:CGRectMake(CGRectGetWidth([contentView bounds])/2 - 100,
CGRectGetMaxY([button frame]) + 10,
200, 14)];
[progress incrementBy:40.0];
[contentView addSubview:progress];
var checkbox = [[CheckboxButton alloc] initWithFrame:CGRectMake(CGRectGetWidth([contentView bounds])/2 - 100,
CGRectGetMaxY([button frame]) + 30,
15,15)];
[contentView addSubview:checkbox];
// test
var view = [[StarRater alloc] initWithFrame:CGRectMake(CGRectGetWidth([contentView bounds])/2 - 100,
CGRectGetMaxY([button frame]) + 50,
200, 25)];
[theWindow setAcceptsMouseMovedEvents:YES];
[contentView addSubview:view];
view._DOMElement.style.cursor = "pointer";
[user setDelegate:view];
[theWindow orderFront:self];
// Uncomment the following line to turn on the standard menu bar.
//[CPMenu setMenuBarVisible:YES];
}
-(void)login:(id)sender
{
var request = [CPURLRequest requestWithURL:"/json/"];
var connection = [CPURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(CPURLConnection) connection didReceiveData:(CPString)data
{
//This method is called when a connection receives a response. in a
//multi-part request, this method will (eventually) be called multiple times,
//once for each part in the response.
alert(data);
}
- (void)connection:(CPURLConnection)connection didFailWithError:(CPString)error
{
//This method is called if the request fails for any reason.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment