PageRenderTime 12ms CodeModel.GetById 8ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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