00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPFont.j"
00024 @import "CPShadow.j"
00025 @import "CPView.j"
00026
00027 #include "Platform/Platform.h"
00028
00029
00030
00031
00032
00033
00034 CPLeftTextAlignment = 0;
00035
00036
00037
00038
00039 CPRightTextAlignment = 1;
00040
00041
00042
00043
00044 CPCenterTextAlignment = 2;
00045
00046
00047
00048
00049 CPJustifiedTextAlignment = 3;
00050
00051
00052
00053
00054 CPNaturalTextAlignment = 4;
00055
00056
00057
00058
00059
00060 CPRegularControlSize = 0;
00061
00062
00063
00064
00065 CPSmallControlSize = 1;
00066
00067
00068
00069
00070 CPMiniControlSize = 2;
00071
00072 CPControlNormalBackgroundColor = "CPControlNormalBackgroundColor";
00073 CPControlSelectedBackgroundColor = "CPControlSelectedBackgroundColor";
00074 CPControlHighlightedBackgroundColor = "CPControlHighlightedBackgroundColor";
00075 CPControlDisabledBackgroundColor = "CPControlDisabledBackgroundColor";
00076
00077 CPControlTextDidBeginEditingNotification = "CPControlTextDidBeginEditingNotification";
00078 CPControlTextDidChangeNotification = "CPControlTextDidChangeNotification";
00079 CPControlTextDidEndEditingNotification = "CPControlTextDidEndEditingNotification";
00080
00081 var CPControlBlackColor = [CPColor blackColor];
00082
00087 @implementation CPControl : CPView
00088 {
00089 id _value;
00090
00091 BOOL _isEnabled;
00092
00093 int _alignment;
00094 CPFont _font;
00095 CPColor _textColor;
00096 CPShadow _textShadow;
00097
00098 id _target;
00099 SEL _action;
00100 int _sendActionOn;
00101
00102 CPDictionary _backgroundColors;
00103 CPString _currentBackgroundColorName;
00104 }
00105
00106 - (id)initWithFrame:(CGRect)aFrame
00107 {
00108 self = [super initWithFrame:aFrame];
00109
00110 if (self)
00111 {
00112 _sendActionOn = CPLeftMouseUpMask;
00113 _isEnabled = YES;
00114
00115 [self setFont:[CPFont systemFontOfSize:12.0]];
00116 [self setTextColor:CPControlBlackColor];
00117
00118 _backgroundColors = [CPDictionary dictionary];
00119 }
00120
00121 return self;
00122 }
00123
00128 - (void)setEnabled:(BOOL)isEnabled
00129 {
00130 [self setAlphaValue:(_isEnabled = isEnabled) ? 1.0 : 0.3];
00131 }
00132
00136 - (BOOL)isEnabled
00137 {
00138 return _isEnabled;
00139 }
00140
00141
00145 - (void)setTextColor:(CPColor)aColor
00146 {
00147 if (_textColor == aColor)
00148 return;
00149
00150 _textColor = aColor;
00151
00152 #if PLATFORM(DOM)
00153 _DOMElement.style.color = [aColor cssString];
00154 #endif
00155 }
00156
00160 - (CPColor)textColor
00161 {
00162 return _textColor;
00163 }
00164
00168 - (CPTextAlignment)alignment
00169 {
00170 return _alignment;
00171 }
00172
00177 - (void)setAlignment:(CPTextAlignment)anAlignment
00178 {
00179 _alignment = anAlignment;
00180 }
00181
00186 - (void)setFont:(CPFont)aFont
00187 {
00188 if (_font == aFont)
00189 return;
00190
00191 _font = aFont;
00192
00193 #if PLATFORM(DOM)
00194 _DOMElement.style.font = [_font ? _font : [CPFont systemFontOfSize:12.0] cssString];
00195 #endif
00196 }
00197
00201 - (CPFont)font
00202 {
00203 return _font;
00204 }
00205
00210 - (void)setTextShadow:(CPShadow)aTextShadow
00211 {
00212 _DOMElement.style.textShadow = [_textShadow = aTextShadow cssString];
00213 }
00214
00218 - (CPShadow)textShadow
00219 {
00220 return _textShadow;
00221 }
00222
00226 - (SEL)action
00227 {
00228 return _action;
00229 }
00230
00235 - (void)setAction:(SEL)anAction
00236 {
00237 _action = anAction;
00238 }
00239
00243 - (id)target
00244 {
00245 return _target;
00246 }
00247
00252 - (void)setTarget:(id)aTarget
00253 {
00254 _target = aTarget;
00255 }
00256
00257 - (void)mouseUp:(CPEvent)anEvent
00258 {
00259 if (_sendActionOn & CPLeftMouseUpMask && CPRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]))
00260 [self sendAction:_action to:_target];
00261
00262 [super mouseUp:anEvent];
00263 }
00264
00270 - (void)sendAction:(SEL)anAction to:(id)anObject
00271 {
00272 [CPApp sendAction:anAction to:anObject from:self];
00273 }
00274
00275 - (int)sendActionOn:(int)mask
00276 {
00277 var previousMask = _sendActionOn;
00278
00279 _sendActionOn = mask;
00280
00281 return previousMask;
00282 }
00283
00287 - (BOOL)isContinuous
00288 {
00289
00290 return (_sendActionOn & CPPeriodicMask) != 0;
00291 }
00292
00296 - (void)setContinuous:(BOOL)flag
00297 {
00298
00299 if (flag)
00300 _sendActionOn |= CPPeriodicMask;
00301 else
00302 _sendActionOn &= ~CPPeriodicMask;
00303 }
00304
00308 - (id)objectValue
00309 {
00310 return _value;
00311 }
00312
00316 - (void)setObjectValue:(id)anObject
00317 {
00318 _value = anObject;
00319 }
00320
00324 - (float)floatValue
00325 {
00326 var floatValue = parseFloat(_value, 10);
00327 return isNaN(floatValue) ? 0.0 : floatValue;
00328 }
00329
00333 - (void)setFloatValue:(float)aValue
00334 {
00335 [self setObjectValue:aValue];
00336 }
00337
00341 - (double)doubleValue
00342 {
00343 var doubleValue = parseFloat(_value, 10);
00344 return isNaN(doubleValue) ? 0.0 : doubleValue;
00345 }
00346
00350 - (void)setDoubleValue:(double)anObject
00351 {
00352 [self setObjectValue:anObject];
00353 }
00354
00358 - (int)intValue
00359 {
00360 var intValue = parseInt(_value, 10);
00361 return isNaN(intValue) ? 0.0 : intValue;
00362 }
00363
00367 - (void)setIntValue:(int)anObject
00368 {
00369 [self setObjectValue:anObject];
00370 }
00371
00372
00376 - (int)integerValue
00377 {
00378 var intValue = parseInt(_value, 10);
00379 return isNaN(intValue) ? 0.0 : intValue;
00380 }
00381
00385 - (void)setIntegerValue:(int)anObject
00386 {
00387 [self setObjectValue:anObject];
00388 }
00389
00393 - (CPString)stringValue
00394 {
00395 return _value ? String(_value) : "";
00396 }
00397
00401 - (void)setStringValue:(CPString)anObject
00402 {
00403 [self setObjectValue:anObject];
00404 }
00405
00406
00407 - (void)takeDoubleValueFrom:(id)sender
00408 {
00409 if ([sender respondsToSelector:@selector(doubleValue)])
00410 [self setDoubleValue:[sender doubleValue]];
00411 }
00412
00413
00414 - (void)takeFloatValueFrom:(id)sender
00415 {
00416 if ([sender respondsToSelector:@selector(floatValue)])
00417 [self setFloatValue:[sender floatValue]];
00418 }
00419
00420
00421 - (void)takeIntegerValueFrom:(id)sender
00422 {
00423 if ([sender respondsToSelector:@selector(integerValue)])
00424 [self setIntegerValue:[sender integerValue]];
00425 }
00426
00427
00428 - (void)takeIntValueFrom:(id)sender
00429 {
00430 if ([sender respondsToSelector:@selector(intValue)])
00431 [self setIntValue:[sender intValue]];
00432 }
00433
00434
00435 - (void)takeObjectValueFrom:(id)sender
00436 {
00437 if ([sender respondsToSelector:@selector(objectValue)])
00438 [self setObjectValue:[sender objectValue]];
00439 }
00440
00441 - (void)takeStringValueFrom:(id)sender
00442 {
00443 if ([sender respondsToSelector:@selector(stringValue)])
00444 [self setStringValue:[sender stringValue]];
00445 }
00446
00447
00448 - (void)setBackgroundColor:(CPColor)aColor
00449 {
00450 _backgroundColors = [CPDictionary dictionary];
00451
00452 [self setBackgroundColor:aColor forName:CPControlNormalBackgroundColor];
00453
00454 [super setBackgroundColor:aColor];
00455 }
00456
00457 - (void)setBackgroundColor:(CPColor)aColor forName:(CPString)aName
00458 {
00459 if (!aColor)
00460 [_backgroundColors removeObjectForKey:aName];
00461 else
00462 [_backgroundColors setObject:aColor forKey:aName];
00463
00464 if (_currentBackgroundColorName == aName)
00465 [self setBackgroundColorWithName:_currentBackgroundColorName];
00466 }
00467
00468 - (CPColor)backgroundColorForName:(CPString)aName
00469 {
00470 var backgroundColor = [_backgroundColors objectForKey:aName];
00471
00472 if (!backgroundColor && aName != CPControlNormalBackgroundColor)
00473 return [_backgroundColors objectForKey:CPControlNormalBackgroundColor];
00474
00475 return backgroundColor;
00476 }
00477
00478 - (void)setBackgroundColorWithName:(CPString)aName
00479 {
00480 _currentBackgroundColorName = aName;
00481
00482 [super setBackgroundColor:[self backgroundColorForName:aName]];
00483 }
00484
00485 - (void)textDidBeginEditing:(CPNotification)note
00486 {
00487
00488 if([note object] != self)
00489 return;
00490
00491 [[CPNotificationCenter defaultCenter] postNotificationName:CPControlTextDidBeginEditingNotification object:self userInfo:[CPDictionary dictionaryWithObject:[note object] forKey:"CPFieldEditor"]];
00492 }
00493
00494 - (void)textDidChange:(CPNotification)note
00495 {
00496
00497 if([note object] != self)
00498 return;
00499
00500 [[CPNotificationCenter defaultCenter] postNotificationName:CPControlTextDidChangeNotification object:self userInfo:[CPDictionary dictionaryWithObject:[note object] forKey:"CPFieldEditor"]];
00501 }
00502
00503 - (void)textDidEndEditing:(CPNotification)note
00504 {
00505
00506 if([note object] != self)
00507 return;
00508
00509 [[CPNotificationCenter defaultCenter] postNotificationName:CPControlTextDidEndEditingNotification object:self userInfo:[CPDictionary dictionaryWithObject:[note object] forKey:"CPFieldEditor"]];
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 @end
00527
00528 var CPControlValueKey = "CPControlValueKey",
00529 CPControlIsEnabledKey = "CPControlIsEnabledKey",
00530 CPControlAlignmentKey = "CPControlAlignmentKey",
00531 CPControlFontKey = "CPControlFontKey",
00532 CPControlTextColorKey = "CPControlTextColorKey",
00533 CPControlTargetKey = "CPControlTargetKey",
00534 CPControlActionKey = "CPControlActionKey",
00535 CPControlSendActionOnKey = "CPControlSendActionOnKey";
00536
00537 @implementation CPControl (CPCoding)
00538
00539
00540
00541
00542
00543
00544 - (id)initWithCoder:(CPCoder)aCoder
00545 {
00546 self = [super initWithCoder:aCoder];
00547
00548 if (self)
00549 {
00550 [self setObjectValue:[aCoder decodeObjectForKey:CPControlValueKey]];
00551
00552 [self setEnabled:[aCoder decodeBoolForKey:CPControlIsEnabledKey]];
00553
00554 [self setAlignment:[aCoder decodeIntForKey:CPControlAlignmentKey]];
00555 [self setFont:[aCoder decodeObjectForKey:CPControlFontKey]];
00556 [self setTextColor:[aCoder decodeObjectForKey:CPControlTextColorKey]];
00557
00558 [self setTarget:[aCoder decodeObjectForKey:CPControlTargetKey]];
00559 [self setAction:[aCoder decodeObjectForKey:CPControlActionKey]];
00560 [self sendActionOn:[aCoder decodeIntForKey:CPControlSendActionOnKey]];
00561 }
00562
00563 return self;
00564 }
00565
00566
00567
00568
00569
00570 - (void)encodeWithCoder:(CPCoder)aCoder
00571 {
00572 [super encodeWithCoder:aCoder];
00573
00574 [aCoder encodeObject:_value forKey:CPControlValueKey];
00575
00576 [aCoder encodeBool:_isEnabled forKey:CPControlIsEnabledKey];
00577
00578 [aCoder encodeInt:_alignment forKey:CPControlAlignmentKey];
00579 [aCoder encodeObject:_font forKey:CPControlFontKey];
00580 [aCoder encodeObject:_textColor forKey:CPControlTextColorKey];
00581
00582 [aCoder encodeConditionalObject:_target forKey:CPControlTargetKey];
00583 [aCoder encodeObject:_action forKey:CPControlActionKey];
00584
00585 [aCoder encodeInt:_sendActionOn forKey:CPControlSendActionOnKey];
00586 }
00587
00588 @end
00589
00590 var _CPControlSizeIdentifiers = [],
00591 _CPControlCachedThreePartImages = {},
00592 _CPControlCachedColorWithPatternImages = {},
00593 _CPControlCachedThreePartImagePattern = {};
00594
00595 _CPControlSizeIdentifiers[CPRegularControlSize] = "Regular";
00596 _CPControlSizeIdentifiers[CPSmallControlSize] = "Small";
00597 _CPControlSizeIdentifiers[CPMiniControlSize] = "Mini";
00598
00599 function _CPControlIdentifierForControlSize(aControlSize)
00600 {
00601 return _CPControlSizeIdentifiers[aControlSize];
00602 }
00603
00604 function _CPControlColorWithPatternImage(sizes, aClassName)
00605 {
00606 var index = 1,
00607 count = arguments.length,
00608 identifier = "";
00609
00610 for (; index < count; ++index)
00611 identifier += arguments[index];
00612
00613 var color = _CPControlCachedColorWithPatternImages[identifier];
00614
00615 if (!color)
00616 {
00617 var bundle = [CPBundle bundleForClass:[CPControl class]];
00618
00619 color = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:aClassName + "/" + identifier + ".png"] size:sizes[identifier]]];
00620
00621 _CPControlCachedColorWithPatternImages[identifier] = color;
00622 }
00623
00624 return color;
00625 }
00626
00627 function _CPControlThreePartImages(sizes, aClassName)
00628 {
00629 var index = 1,
00630 count = arguments.length,
00631 identifier = "";
00632
00633 for (; index < count; ++index)
00634 identifier += arguments[index];
00635
00636 var images = _CPControlCachedThreePartImages[identifier];
00637
00638 if (!images)
00639 {
00640 var bundle = [CPBundle bundleForClass:[CPControl class]],
00641 path = aClassName + "/" + identifier;
00642
00643 sizes = sizes[identifier];
00644
00645 images = [
00646 [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:path + "0.png"] size:sizes[0]],
00647 [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:path + "1.png"] size:sizes[1]],
00648 [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:path + "2.png"] size:sizes[2]]
00649 ];
00650
00651 _CPControlCachedThreePartImages[identifier] = images;
00652 }
00653
00654 return images;
00655 }
00656
00657 function _CPControlThreePartImagePattern(isVertical, sizes, aClassName)
00658 {
00659 var index = 2,
00660 count = arguments.length,
00661 identifier = "";
00662
00663 for (; index < count; ++index)
00664 identifier += arguments[index];
00665
00666 var color = _CPControlCachedThreePartImagePattern[identifier];
00667
00668 if (!color)
00669 {
00670 var bundle = [CPBundle bundleForClass:[CPControl class]],
00671 path = aClassName + "/" + identifier;
00672
00673 sizes = sizes[identifier];
00674
00675 color = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
00676 [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:path + "0.png"] size:sizes[0]],
00677 [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:path + "1.png"] size:sizes[1]],
00678 [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:path + "2.png"] size:sizes[2]]
00679 ] isVertical:isVertical]];
00680
00681 _CPControlCachedThreePartImagePattern[identifier] = color;
00682 }
00683
00684 return color;
00685 }
00686