OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/cocoa/bookmark_button_cell.h" | 5 #import "chrome/browser/cocoa/bookmark_button_cell.h" |
6 | 6 |
7 #include "app/l10n_util_mac.h" | 7 #include "app/l10n_util_mac.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/nsimage_cache_mac.h" |
9 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
10 #import "chrome/browser/bookmarks/bookmark_model.h" | 11 #import "chrome/browser/bookmarks/bookmark_model.h" |
11 #import "chrome/browser/cocoa/bookmark_menu.h" | 12 #import "chrome/browser/cocoa/bookmark_menu.h" |
| 13 #import "chrome/browser/cocoa/bookmark_button.h" |
12 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
13 | 15 |
14 | 16 |
15 @interface BookmarkButtonCell(Private) | 17 @interface BookmarkButtonCell(Private) |
16 - (void)configureBookmarkButtonCell; | 18 - (void)configureBookmarkButtonCell; |
17 @end | 19 @end |
18 | 20 |
19 | 21 |
20 @implementation BookmarkButtonCell | 22 @implementation BookmarkButtonCell |
21 | 23 |
22 @synthesize startingChildIndex = startingChildIndex_; | 24 @synthesize startingChildIndex = startingChildIndex_; |
| 25 @synthesize drawFolderArrow = drawFolderArrow_; |
23 | 26 |
24 + (id)buttonCellForNode:(const BookmarkNode*)node | 27 + (id)buttonCellForNode:(const BookmarkNode*)node |
25 contextMenu:(NSMenu*)contextMenu | 28 contextMenu:(NSMenu*)contextMenu |
26 cellText:(NSString*)cellText | 29 cellText:(NSString*)cellText |
27 cellImage:(NSImage*)cellImage { | 30 cellImage:(NSImage*)cellImage { |
28 id buttonCell = | 31 id buttonCell = |
29 [[[BookmarkButtonCell alloc] initForNode:node | 32 [[[BookmarkButtonCell alloc] initForNode:node |
30 contextMenu:contextMenu | 33 contextMenu:contextMenu |
31 cellText:cellText | 34 cellText:cellText |
32 cellImage:cellImage] | 35 cellImage:cellImage] |
(...skipping 13 matching lines...) Expand all Loading... |
46 if (node) { | 49 if (node) { |
47 NSString* title = base::SysWideToNSString(node->GetTitle()); | 50 NSString* title = base::SysWideToNSString(node->GetTitle()); |
48 [self setBookmarkCellText:title image:cellImage]; | 51 [self setBookmarkCellText:title image:cellImage]; |
49 [self setMenu:contextMenu]; | 52 [self setMenu:contextMenu]; |
50 } else { | 53 } else { |
51 [self setEmpty:YES]; | 54 [self setEmpty:YES]; |
52 [self setBookmarkCellText:l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU) | 55 [self setBookmarkCellText:l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU) |
53 image:nil]; | 56 image:nil]; |
54 } | 57 } |
55 } | 58 } |
| 59 |
56 return self; | 60 return self; |
57 } | 61 } |
58 | 62 |
59 - (id)initTextCell:(NSString*)string { | 63 - (id)initTextCell:(NSString*)string { |
60 return [self initForNode:nil contextMenu:nil cellText:string cellImage:nil]; | 64 return [self initForNode:nil contextMenu:nil cellText:string cellImage:nil]; |
61 } | 65 } |
62 | 66 |
63 // Used by the off-the-side menu, the only case where a | 67 // Used by the off-the-side menu, the only case where a |
64 // BookmarkButtonCell is loaded from a nib. | 68 // BookmarkButtonCell is loaded from a nib. |
65 - (void)awakeFromNib { | 69 - (void)awakeFromNib { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 [super mouseEntered:event]; | 173 [super mouseEntered:event]; |
170 [[self controlView] mouseEntered:event]; | 174 [[self controlView] mouseEntered:event]; |
171 } | 175 } |
172 | 176 |
173 // See comment above mouseEntered:, above. | 177 // See comment above mouseEntered:, above. |
174 - (void)mouseExited:(NSEvent*)event { | 178 - (void)mouseExited:(NSEvent*)event { |
175 [super mouseExited:event]; | 179 [super mouseExited:event]; |
176 [[self controlView] mouseExited:event]; | 180 [[self controlView] mouseExited:event]; |
177 } | 181 } |
178 | 182 |
| 183 - (void)setDrawFolderArrow:(BOOL)draw { |
| 184 drawFolderArrow_ = draw; |
| 185 if (draw && !arrowImage_) { |
| 186 arrowImage_.reset([nsimage_cache::ImageNamed(@"menu_hierarchy_arrow.pdf") |
| 187 retain]); |
| 188 } |
| 189 } |
| 190 |
| 191 // Add extra size for the arrow so it doesn't overlap the text. |
| 192 // Does not sanity check to be sure this is actually a folder node. |
| 193 - (NSSize)cellSize { |
| 194 NSSize cellSize = [super cellSize]; |
| 195 if (drawFolderArrow_) { |
| 196 cellSize.width += [arrowImage_ size].width; // plus margin? |
| 197 } |
| 198 return cellSize; |
| 199 } |
| 200 |
| 201 // Override cell drawing to add a submenu arrow like a real menu. |
| 202 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 203 // First draw "everything else". |
| 204 [super drawInteriorWithFrame:cellFrame inView:controlView]; |
| 205 |
| 206 // If asked to do so, and if a folder, draw the arrow. |
| 207 if (!drawFolderArrow_) |
| 208 return; |
| 209 BookmarkButton* button = static_cast<BookmarkButton*>([self controlView]); |
| 210 DCHECK([button respondsToSelector:@selector(isFolder)]); |
| 211 if ([button isFolder]) { |
| 212 NSRect imageRect = NSZeroRect; |
| 213 imageRect.size = [arrowImage_ size]; |
| 214 NSRect drawRect = NSOffsetRect(imageRect, |
| 215 NSWidth(cellFrame) - NSWidth(imageRect), |
| 216 (NSHeight(cellFrame) / 2.0) - |
| 217 (NSHeight(imageRect) / 2.0)); |
| 218 [arrowImage_ setFlipped:[controlView isFlipped]]; |
| 219 [arrowImage_ drawInRect:drawRect |
| 220 fromRect:imageRect |
| 221 operation:NSCompositeSourceOver |
| 222 fraction:[self isEnabled] ? 1.0 : 0.5]; |
| 223 } |
| 224 } |
| 225 |
179 @end | 226 @end |
OLD | NEW |