PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/Tools/nib2cib/NSControl.j

http://github.com/cacaodev/cappuccino
Unknown | 113 lines | 90 code | 23 blank | 0 comment | 0 complexity | 68b062baaabfa29df503f1efb3b6af8e MD5 | raw file
  1/*
  2 * NSControl.j
  3 * nib2cib
  4 *
  5 * Created by Francisco Tolmasky.
  6 * Copyright 2008, 280 North, Inc.
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2.1 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 16 * Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 21 */
 22
 23@import <AppKit/CPControl.j>
 24
 25@import "NSCell.j"
 26@import "NSView.j"
 27
 28
 29@class Nib2Cib
 30
 31
 32@implementation CPControl (NSCoding)
 33
 34- (id)NS_initWithCoder:(CPCoder)aCoder
 35{
 36    self = [super NS_initWithCoder:aCoder];
 37
 38    if (self)
 39    {
 40        // Enabled state is derived from the NSEnabled flag or the control's cell.
 41        // For example NSTableView uses the NSEnabled flag, but NSButton uses it's cell isEnabled state.
 42        // We use the NSEnabled flag here and override the behavior in controls using different logic (NSButton).
 43        [self setEnabled:[aCoder decodeBoolForKey:@"NSEnabled"]];
 44
 45        [self sendActionOn:CPLeftMouseUpMask];
 46        [self setTarget:[aCoder decodeObjectForKey:@"NSTarget"]];
 47        [self setAction:[aCoder decodeObjectForKey:@"NSAction"]];
 48
 49        // In IB, both cells and controls can have tags.
 50        // If the control has a tag, that takes precedence.
 51        if ([aCoder containsValueForKey:@"NSTag"])
 52            [self setTag:[aCoder decodeIntForKey:@"NSTag"]];
 53    }
 54
 55    return self;
 56}
 57
 58- (void)NS_initWithCell:(NSCell)cell
 59{
 60    [self setSendsActionOnEndEditing:[cell sendsActionOnEndEditing]];
 61    [self setObjectValue:[cell objectValue]];
 62    [self setFont:[cell font]];
 63    [self setAlignment:[cell alignment]];
 64    [self setContinuous:[cell isContinuous]];
 65    [self setLineBreakMode:[cell lineBreakMode]];
 66    [self setFormatter:[cell formatter]];
 67    [self setControlSize:[cell controlSize]];
 68}
 69
 70- (CGRect)_nib2CibAdjustment
 71{
 72    // Theme has not been loaded yet.
 73    // Get attribute value directly from the theme or from the default value of the object otherwise.
 74    var theme = [Nib2Cib defaultTheme];
 75
 76    return [theme valueForAttributeWithName:@"nib2cib-adjustment-frame" inState:[self themeState] forClass:[self class]] || [self currentValueForThemeAttribute:@"nib2cib-adjustment-frame"];
 77}
 78
 79- (void)_adjustNib2CibSize
 80{
 81    var frame = [self frame],
 82        frameAdjustment = [self _nib2CibAdjustment];
 83
 84    if (frameAdjustment)
 85    {
 86        var finalFrame = CGRectMake(frame.origin.x + frameAdjustment.origin.x, frame.origin.y - frameAdjustment.origin.y, frame.size.width + frameAdjustment.size.width, frame.size.height + frameAdjustment.size.height);
 87        [self setFrame:finalFrame];
 88    }
 89}
 90
 91@end
 92
 93@implementation NSControl : CPControl
 94
 95- (id)initWithCoder:(CPCoder)aCoder
 96{
 97    self = [self NS_initWithCoder:aCoder];
 98
 99    if (self)
100    {
101        var cell = [aCoder decodeObjectForKey:@"NSCell"];
102        [self NS_initWithCell:cell];
103    }
104
105    return self;
106}
107
108- (Class)classForKeyedArchiver
109{
110    return [CPControl class];
111}
112
113@end