/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. @import <AppKit/CPControl.j>
  23. @import "NSCell.j"
  24. @import "NSView.j"
  25. @class Nib2Cib
  26. @implementation CPControl (NSCoding)
  27. - (id)NS_initWithCoder:(CPCoder)aCoder
  28. {
  29. self = [super NS_initWithCoder:aCoder];
  30. if (self)
  31. {
  32. // Enabled state is derived from the NSEnabled flag or the control's cell.
  33. // For example NSTableView uses the NSEnabled flag, but NSButton uses it's cell isEnabled state.
  34. // We use the NSEnabled flag here and override the behavior in controls using different logic (NSButton).
  35. [self setEnabled:[aCoder decodeBoolForKey:@"NSEnabled"]];
  36. [self sendActionOn:CPLeftMouseUpMask];
  37. [self setTarget:[aCoder decodeObjectForKey:@"NSTarget"]];
  38. [self setAction:[aCoder decodeObjectForKey:@"NSAction"]];
  39. // In IB, both cells and controls can have tags.
  40. // If the control has a tag, that takes precedence.
  41. if ([aCoder containsValueForKey:@"NSTag"])
  42. [self setTag:[aCoder decodeIntForKey:@"NSTag"]];
  43. }
  44. return self;
  45. }
  46. - (void)NS_initWithCell:(NSCell)cell
  47. {
  48. [self setSendsActionOnEndEditing:[cell sendsActionOnEndEditing]];
  49. [self setObjectValue:[cell objectValue]];
  50. [self setFont:[cell font]];
  51. [self setAlignment:[cell alignment]];
  52. [self setContinuous:[cell isContinuous]];
  53. [self setLineBreakMode:[cell lineBreakMode]];
  54. [self setFormatter:[cell formatter]];
  55. [self setControlSize:[cell controlSize]];
  56. }
  57. - (CGRect)_nib2CibAdjustment
  58. {
  59. // Theme has not been loaded yet.
  60. // Get attribute value directly from the theme or from the default value of the object otherwise.
  61. var theme = [Nib2Cib defaultTheme];
  62. return [theme valueForAttributeWithName:@"nib2cib-adjustment-frame" inState:[self themeState] forClass:[self class]] || [self currentValueForThemeAttribute:@"nib2cib-adjustment-frame"];
  63. }
  64. - (void)_adjustNib2CibSize
  65. {
  66. var frame = [self frame],
  67. frameAdjustment = [self _nib2CibAdjustment];
  68. if (frameAdjustment)
  69. {
  70. 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);
  71. [self setFrame:finalFrame];
  72. }
  73. }
  74. @end
  75. @implementation NSControl : CPControl
  76. - (id)initWithCoder:(CPCoder)aCoder
  77. {
  78. self = [self NS_initWithCoder:aCoder];
  79. if (self)
  80. {
  81. var cell = [aCoder decodeObjectForKey:@"NSCell"];
  82. [self NS_initWithCell:cell];
  83. }
  84. return self;
  85. }
  86. - (Class)classForKeyedArchiver
  87. {
  88. return [CPControl class];
  89. }
  90. @end