/AppKit/CPMenuItem/_CPMenuItemView.j

http://github.com/cacaodev/cappuccino · Unknown · 285 lines · 228 code · 57 blank · 0 comment · 0 complexity · 3c19ac8e790f921a0e44a82573c37943 MD5 · raw file

  1. /*
  2. * _CPMenuItemView.j
  3. * AppKit
  4. *
  5. * Created by Francisco Tolmasky.
  6. * Copyright 2009, 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 "CPControl.j"
  23. @import "_CPMenuItemSeparatorView.j"
  24. @import "_CPMenuItemStandardView.j"
  25. @import "_CPMenuItemMenuBarView.j"
  26. @class CPMenuItem
  27. @global CPApp
  28. /*
  29. @ignore
  30. */
  31. @implementation _CPMenuItemView : CPView
  32. {
  33. CPMenuItem _menuItem;
  34. CPView _view @accessors(property=view, readonly);
  35. CPFont _font;
  36. CPColor _textColor;
  37. CPColor _textShadowColor;
  38. CGSize _minSize;
  39. BOOL _isDirty;
  40. BOOL _showsStateColumn;
  41. _CPImageAndTextView _imageAndTextView;
  42. CPView _submenuView;
  43. }
  44. + (CPString)defaultThemeClass
  45. {
  46. return "menu-item-view";
  47. }
  48. + (CPDictionary)themeAttributes
  49. {
  50. return @{};
  51. }
  52. // Not used in the Appkit
  53. // + (float)leftMargin
  54. // {
  55. // return LEFT_MARGIN + STATE_COLUMN_WIDTH;
  56. // }
  57. - (id)initWithFrame:(CGRect)aFrame forMenuItem:(CPMenuItem)aMenuItem
  58. {
  59. self = [super initWithFrame:aFrame];
  60. if (self)
  61. {
  62. _menuItem = aMenuItem;
  63. _showsStateColumn = YES;
  64. _isDirty = YES;
  65. [self setAutoresizingMask:CPViewWidthSizable];
  66. [self synchronizeWithMenuItem];
  67. }
  68. return self;
  69. }
  70. - (CGSize)minSize
  71. {
  72. return _minSize;
  73. }
  74. - (void)setDirty
  75. {
  76. _isDirty = YES;
  77. }
  78. - (void)synchronizeWithMenuItem
  79. {
  80. var menuItemView = [_menuItem view];
  81. if ([_menuItem isSeparatorItem])
  82. {
  83. if (![_view isKindOfClass:[_CPMenuItemSeparatorView class]])
  84. {
  85. [_view removeFromSuperview];
  86. _view = [_CPMenuItemSeparatorView view];
  87. }
  88. }
  89. else if (menuItemView)
  90. {
  91. if (_view !== menuItemView)
  92. {
  93. [_view removeFromSuperview];
  94. _view = menuItemView;
  95. }
  96. }
  97. else if ([_menuItem menu] == [CPApp mainMenu])
  98. {
  99. if (![_view isKindOfClass:[_CPMenuItemMenuBarView class]])
  100. {
  101. [_view removeFromSuperview];
  102. _view = [_CPMenuItemMenuBarView view];
  103. }
  104. [_view setMenuItem:_menuItem];
  105. }
  106. else
  107. {
  108. if (![_view isKindOfClass:[_CPMenuItemStandardView class]])
  109. {
  110. [_view removeFromSuperview];
  111. _view = [_CPMenuItemStandardView view];
  112. }
  113. [_view setMenuItem:_menuItem];
  114. }
  115. if ([_view superview] !== self)
  116. [self addSubview:_view];
  117. if ([_view respondsToSelector:@selector(update)])
  118. [_view update];
  119. _minSize = [_view frame].size;
  120. [self setAutoresizesSubviews:NO];
  121. [self setFrameSize:_minSize];
  122. [self setAutoresizesSubviews:YES];
  123. }
  124. - (void)setShowsStateColumn:(BOOL)shouldShowStateColumn
  125. {
  126. _showsStateColumn = shouldShowStateColumn;
  127. }
  128. - (void)highlight:(BOOL)shouldHighlight
  129. {
  130. if ([_view respondsToSelector:@selector(highlight:)])
  131. [_view highlight:shouldHighlight];
  132. }
  133. - (BOOL)eventOnSubmenu:(CPEvent)anEvent
  134. {
  135. if (![_menuItem hasSubmenu])
  136. return NO;
  137. return CGRectContainsPoint([_submenuView frame], [self convertPoint:[anEvent locationInWindow] fromView:nil]);
  138. }
  139. - (BOOL)isHidden
  140. {
  141. return [_menuItem isHidden];
  142. }
  143. - (CPMenuItem)menuItem
  144. {
  145. return _menuItem;
  146. }
  147. - (void)setFont:(CPFont)aFont
  148. {
  149. if ([_font isEqual:aFont])
  150. return;
  151. _font = aFont;
  152. if ([_view respondsToSelector:@selector(setFont:)])
  153. [_view setFont:aFont];
  154. [self setDirty];
  155. }
  156. - (void)setTextColor:(CPColor)aColor
  157. {
  158. if (_textColor == aColor)
  159. return;
  160. _textColor = aColor;
  161. [_imageAndTextView setTextColor:[self textColor]];
  162. [_submenuView setColor:[self textColor]];
  163. }
  164. - (CPColor)textColor
  165. {
  166. return nil;
  167. }
  168. - (void)setTextShadowColor:(CPColor)aColor
  169. {
  170. if (_textShadowColor == aColor)
  171. return;
  172. _textShadowColor = aColor;
  173. [_imageAndTextView setTextShadowColor:[self textShadowColor]];
  174. //[_submenuView setColor:[self textColor]];
  175. }
  176. - (CPColor)textShadowColor
  177. {
  178. return [_menuItem isEnabled] ? (_textShadowColor ? _textShadowColor : [CPColor colorWithWhite:1.0 alpha:0.8]) : [CPColor colorWithWhite:0.8 alpha:0.8];
  179. }
  180. - (void)setParentMenuHighlightColor:(CPColor)aColor
  181. {
  182. if ([_view respondsToSelector:@selector(setHighlightColor:)])
  183. [_view setHighlightColor:aColor];
  184. }
  185. - (void)setParentMenuHighlightTextColor:(CPColor)aColor
  186. {
  187. if ([_view respondsToSelector:@selector(setHighlightTextColor:)])
  188. [_view setHighlightTextColor:aColor];
  189. }
  190. - (void)setParentMenuHighlightTextShadowColor:(CPColor)aColor
  191. {
  192. if ([_view respondsToSelector:@selector(setHighlightTextShadowColor:)])
  193. [_view setHighlightTextShadowColor:aColor];
  194. }
  195. - (void)setParentMenuTextColor:(CPColor)aColor
  196. {
  197. if ([_view respondsToSelector:@selector(setTextColor:)])
  198. [_view setTextColor:aColor];
  199. }
  200. - (void)setParentMenuTextShadowColor:(CPColor)aColor
  201. {
  202. if ([_view respondsToSelector:@selector(setTextShadowColor:)])
  203. [_view setTextShadowColor:aColor];
  204. }
  205. @end
  206. @implementation _CPMenuItemArrowView : CPView
  207. {
  208. CPColor _color;
  209. }
  210. - (void)setColor:(CPColor)aColor
  211. {
  212. if (_color == aColor)
  213. return;
  214. _color = aColor;
  215. [self setNeedsDisplay:YES];
  216. }
  217. - (void)drawRect:(CGRect)aRect
  218. {
  219. var context = [[CPGraphicsContext currentContext] graphicsPort];
  220. CGContextBeginPath(context);
  221. CGContextMoveToPoint(context, 1.0, 4.0);
  222. CGContextAddLineToPoint(context, 9.0, 4.0);
  223. CGContextAddLineToPoint(context, 5.0, 8.0);
  224. CGContextClosePath(context);
  225. CGContextSetFillColor(context, _color);
  226. CGContextFillPath(context);
  227. }
  228. @end