Guest User

Untitled

a guest
Mar 1st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. /*
  2. * AppController.j
  3. *
  4. * Created by __Me__ on __Date__.
  5. * Copyright 2008 __MyCompanyName__. All rights reserved.
  6. */
  7.  
  8. @import <Foundation/CPObject.j>
  9. @import "Checkbox.j"
  10.  
  11. var starEmpty, starSet, starActive;
  12.  
  13. @implementation StarRater : CPView
  14. {
  15. int _value;
  16. int _hoverStars;
  17. CPView _starFrame, _hoverFrame;
  18. id _delegate;
  19. }
  20. + (void)initialize
  21. {
  22. starEmpty = [[CPImage alloc] initWithContentsOfFile: "Resources/StarRater/empty.gif" size: CPSizeMake(25, 25)];
  23. starSet = [[CPImage alloc] initWithContentsOfFile: "Resources/StarRater/set.gif" size: CPSizeMake(25, 25)];
  24. starActive = [[CPImage alloc] initWithContentsOfFile: "Resources/StarRater/active.gif" size: CPSizeMake(25, 25)];
  25. }
  26. - (id)initWithFrame:(CPRect)frame
  27. {
  28. self = [super initWithFrame: frame];
  29.  
  30. //debugger;
  31.  
  32. _starFrame = [[CPView alloc] initWithFrame:[self bounds]];
  33. [_starFrame setBackgroundColor:[CPColor colorWithPatternImage:starSet]];
  34.  
  35. _hoverFrame = [[CPView alloc] initWithFrame:[self bounds]];
  36. [_hoverFrame setBackgroundColor:[CPColor colorWithPatternImage:starActive]];
  37.  
  38. _hoverStars = 0;
  39.  
  40. [self setBackgroundColor:[CPColor colorWithPatternImage:starEmpty]];
  41.  
  42. [self _redraw];
  43. [self _hover];
  44. [self addSubview: _starFrame];
  45. [self addSubview: _hoverFrame];
  46. return self;
  47. }
  48.  
  49. - (void)setValue:(int)value
  50. {
  51. _value = value;
  52. }
  53.  
  54. - (int)intValue
  55. {
  56. return _value;
  57. }
  58.  
  59. - (void)setDelegate:(id)aDelegate
  60. {
  61. if(_delegate == aDelegate)
  62. return;
  63. _delegate = aDelegate;
  64. }
  65. - (id)delegate
  66. {
  67. return _delegate;
  68. }
  69. - (void)_redraw
  70. {
  71. [_starFrame setFrameSize:CPSizeMake(_value * 25, 25)];
  72. }
  73.  
  74. - (void)_hover
  75. {
  76. [_hoverFrame setFrameSize:CPSizeMake(_hoverStars * 25, 25)];
  77. }
  78.  
  79. - (void)mouseDown:(CPEvent)anEvent
  80. {
  81. var px_diff = [anEvent locationInWindow].x - CGRectGetMinX([self frame]);
  82. if(px_diff < 5) [self setValue:0];
  83. else [self setValue:Math.ceil(px_diff/25)];
  84. _hoverStars = 0;
  85. [self _redraw];
  86. [self _hover];
  87. }
  88.  
  89. - (void)mouseExited:(CPEvent)anEvent
  90. {
  91. _hoverStars = 0;
  92. [self _hover];
  93. }
  94.  
  95. - (void)mouseEntered:(CPEvent)anEvent
  96. {
  97. _hoverStars = 0;
  98. [self _hover];
  99. }
  100.  
  101. - (void)mouseMoved:(CPEvent)anEvent
  102. {
  103. var px_diff = [anEvent locationInWindow].x - CGRectGetMinX([self frame]);
  104. if(px_diff < 5) starValue = 0;
  105. else starValue = Math.ceil(px_diff/25);
  106. if(starValue == _hoverStars) return;
  107. _hoverStars = starValue;
  108. [self _hover];
  109. }
  110. - (void)controlTextDidChange:(id)aTextField
  111. {
  112. console.log("foo");
  113. console.log([[aTextField stringValue] intValue]);
  114. _value = [[aTextField stringValue] intValue];
  115. [self _redraw];
  116. }
  117.  
  118. @end
  119.  
  120. @implementation AppController : CPObject
  121. {
  122. CPTextField user;
  123. CPTextField password;
  124. }
  125.  
  126. - (void)applicationDidFinishLaunching:(CPNotification)aNotification
  127. {
  128.  
  129. var field_length = @"______________________";
  130.  
  131. var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
  132. contentView = [theWindow contentView];
  133.  
  134. // user field
  135.  
  136. var userLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
  137. [userLabel setStringValue:@"Benutzer"];
  138. [userLabel sizeToFit];
  139.  
  140. [userLabel setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
  141.  
  142. user = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
  143.  
  144. [user setStringValue:field_length];
  145. [user sizeToFit];
  146.  
  147. [user setStringValue:@""];
  148. [user setBordered:YES];
  149. [user setEditable:YES];
  150. [user setBezeled:YES];
  151. [user setBezelStyle:CPTextFieldSquareBezel];
  152.  
  153. [user setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
  154.  
  155. // password field
  156.  
  157. var passwordLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
  158. [passwordLabel setStringValue:@"Kennwort"];
  159. [passwordLabel sizeToFit];
  160.  
  161. [passwordLabel setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
  162.  
  163. password = [[CPSecureTextField alloc] initWithFrame:CGRectMakeZero()];
  164.  
  165. [password setStringValue:field_length];
  166. [password sizeToFit];
  167.  
  168. [password setStringValue:@""];
  169. [password setBordered:YES];
  170. [password setEditable:YES];
  171. [password setBezeled:YES];
  172. [password setBezelStyle:CPTextFieldSquareBezel];
  173.  
  174. [password setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
  175.  
  176. // Laying out the fields
  177.  
  178. // wow, this function is not build in...
  179. function max(a,b) { return a > b ? a : b; }
  180.  
  181. var Luser_w = CGRectGetWidth([userLabel frame]);
  182. var user_w = CGRectGetWidth([user frame]);
  183. var Lpass_w = CGRectGetWidth([passwordLabel frame]);
  184. var pass_w = CGRectGetWidth([password frame]);
  185. var base_w = max(Luser_w,Lpass_w);
  186.  
  187. [userLabel setFrameOrigin:CGPointMake(((CGRectGetWidth([contentView bounds]) - base_w - user_w - 10)/2
  188. + base_w - Luser_w),
  189. (CGRectGetHeight([contentView bounds])/2 - CGRectGetHeight([userLabel frame])))];
  190.  
  191. [user setFrameOrigin:CGPointMake(((CGRectGetWidth([contentView bounds]) - base_w - user_w - 10)/2
  192. + base_w + 5),
  193. (CGRectGetHeight([contentView bounds])/2 - CGRectGetHeight([user frame])))];
  194.  
  195. [passwordLabel setFrameOrigin:CGPointMake(((CGRectGetWidth([contentView bounds]) - base_w - user_w - 10)/2
  196. + base_w - Lpass_w),
  197. (CGRectGetHeight([contentView bounds])/2 + 10))];
  198.  
  199. [password setFrameOrigin:CGPointMake(((CGRectGetWidth([contentView bounds]) - base_w - user_w - 10)/2
  200. + base_w + 5),
  201. (CGRectGetHeight([contentView bounds])/2 + 10))];
  202.  
  203. [contentView addSubview:userLabel];
  204. [contentView addSubview:user];
  205.  
  206. [contentView addSubview:passwordLabel];
  207. [contentView addSubview:password];
  208.  
  209. // add the login button
  210. var button = [[CPButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX([password frame]) - 80.0,
  211. CGRectGetMaxY([password frame]) + 10,
  212. 80, 18)];
  213.  
  214. [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
  215.  
  216. [button setTarget:self];
  217. [button setAction:@selector(login:)];
  218.  
  219. [button setTitle:"Anmelden"];
  220.  
  221. [contentView addSubview:button];
  222.  
  223.  
  224. var progress = [[CPProgressIndicator alloc] initWithFrame:CGRectMake(CGRectGetWidth([contentView bounds])/2 - 100,
  225. CGRectGetMaxY([button frame]) + 10,
  226. 200, 14)];
  227. [progress incrementBy:40.0];
  228. [contentView addSubview:progress];
  229.  
  230. var checkbox = [[CheckboxButton alloc] initWithFrame:CGRectMake(CGRectGetWidth([contentView bounds])/2 - 100,
  231. CGRectGetMaxY([button frame]) + 30,
  232. 15,15)];
  233.  
  234. [contentView addSubview:checkbox];
  235.  
  236.  
  237. // test
  238. var view = [[StarRater alloc] initWithFrame:CGRectMake(CGRectGetWidth([contentView bounds])/2 - 100,
  239. CGRectGetMaxY([button frame]) + 50,
  240. 200, 25)];
  241. [theWindow setAcceptsMouseMovedEvents:YES];
  242.  
  243.  
  244. [contentView addSubview:view];
  245. view._DOMElement.style.cursor = "pointer";
  246.  
  247. [user setDelegate:view];
  248.  
  249. [theWindow orderFront:self];
  250.  
  251.  
  252. // Uncomment the following line to turn on the standard menu bar.
  253. //[CPMenu setMenuBarVisible:YES];
  254. }
  255.  
  256. -(void)login:(id)sender
  257. {
  258. var request = [CPURLRequest requestWithURL:"/json/"];
  259. var connection = [CPURLConnection connectionWithRequest:request delegate:self];
  260. }
  261.  
  262. - (void)connection:(CPURLConnection) connection didReceiveData:(CPString)data
  263. {
  264. //This method is called when a connection receives a response. in a
  265. //multi-part request, this method will (eventually) be called multiple times,
  266. //once for each part in the response.
  267. alert(data);
  268. }
  269.  
  270. - (void)connection:(CPURLConnection)connection didFailWithError:(CPString)error
  271. {
  272. //This method is called if the request fails for any reason.
  273. }
  274.  
  275. @end
Add Comment
Please, Sign In to add comment