| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/cocoa/bookmarks/bookmark_button_cell.h" | 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #import "chrome/browser/bookmarks/bookmark_model.h" | 9 #import "chrome/browser/bookmarks/bookmark_model.h" |
| 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" | 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" |
| 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu.h" | 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu.h" |
| 12 #import "chrome/browser/ui/cocoa/image_utils.h" | 12 #import "chrome/browser/ui/cocoa/image_utils.h" |
| 13 #include "content/browser/user_metrics.h" | 13 #include "content/browser/user_metrics.h" |
| 14 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 15 #include "ui/base/l10n/l10n_util_mac.h" | 15 #include "ui/base/l10n/l10n_util_mac.h" |
| 16 #include "ui/gfx/mac/nsimage_cache.h" |
| 16 | 17 |
| 17 | 18 |
| 18 @interface BookmarkButtonCell(Private) | 19 @interface BookmarkButtonCell(Private) |
| 19 - (void)configureBookmarkButtonCell; | 20 - (void)configureBookmarkButtonCell; |
| 20 - (void)applyTextColor; | 21 - (void)applyTextColor; |
| 21 @end | 22 @end |
| 22 | 23 |
| 23 | 24 |
| 24 @implementation BookmarkButtonCell | 25 @implementation BookmarkButtonCell |
| 25 | 26 |
| 27 @synthesize startingChildIndex = startingChildIndex_; |
| 28 @synthesize drawFolderArrow = drawFolderArrow_; |
| 29 |
| 26 + (id)buttonCellForNode:(const BookmarkNode*)node | 30 + (id)buttonCellForNode:(const BookmarkNode*)node |
| 27 contextMenu:(NSMenu*)contextMenu | 31 contextMenu:(NSMenu*)contextMenu |
| 28 cellText:(NSString*)cellText | 32 cellText:(NSString*)cellText |
| 29 cellImage:(NSImage*)cellImage { | 33 cellImage:(NSImage*)cellImage { |
| 30 id buttonCell = | 34 id buttonCell = |
| 31 [[[BookmarkButtonCell alloc] initForNode:node | 35 [[[BookmarkButtonCell alloc] initForNode:node |
| 32 contextMenu:contextMenu | 36 contextMenu:contextMenu |
| 33 cellText:cellText | 37 cellText:cellText |
| 34 cellImage:cellImage] | 38 cellImage:cellImage] |
| 35 autorelease]; | 39 autorelease]; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 62 - (id)initTextCell:(NSString*)string { | 66 - (id)initTextCell:(NSString*)string { |
| 63 return [self initForNode:nil contextMenu:nil cellText:string cellImage:nil]; | 67 return [self initForNode:nil contextMenu:nil cellText:string cellImage:nil]; |
| 64 } | 68 } |
| 65 | 69 |
| 66 // Used by the off-the-side menu, the only case where a | 70 // Used by the off-the-side menu, the only case where a |
| 67 // BookmarkButtonCell is loaded from a nib. | 71 // BookmarkButtonCell is loaded from a nib. |
| 68 - (void)awakeFromNib { | 72 - (void)awakeFromNib { |
| 69 [self configureBookmarkButtonCell]; | 73 [self configureBookmarkButtonCell]; |
| 70 } | 74 } |
| 71 | 75 |
| 76 - (BOOL)isFolderButtonCell { |
| 77 return NO; |
| 78 } |
| 79 |
| 72 // Perform all normal init routines specific to the BookmarkButtonCell. | 80 // Perform all normal init routines specific to the BookmarkButtonCell. |
| 73 - (void)configureBookmarkButtonCell { | 81 - (void)configureBookmarkButtonCell { |
| 74 [self setButtonType:NSMomentaryPushInButton]; | 82 [self setButtonType:NSMomentaryPushInButton]; |
| 75 [self setBezelStyle:NSShadowlessSquareBezelStyle]; | 83 [self setBezelStyle:NSShadowlessSquareBezelStyle]; |
| 76 [self setShowsBorderOnlyWhileMouseInside:YES]; | 84 [self setShowsBorderOnlyWhileMouseInside:YES]; |
| 77 [self setControlSize:NSSmallControlSize]; | 85 [self setControlSize:NSSmallControlSize]; |
| 78 [self setAlignment:NSLeftTextAlignment]; | 86 [self setAlignment:NSLeftTextAlignment]; |
| 79 [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | 87 [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; |
| 80 [self setWraps:NO]; | 88 [self setWraps:NO]; |
| 81 // NSLineBreakByTruncatingMiddle seems more common on OSX but let's | 89 // NSLineBreakByTruncatingMiddle seems more common on OSX but let's |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 [super mouseEntered:event]; | 201 [super mouseEntered:event]; |
| 194 [[self controlView] mouseEntered:event]; | 202 [[self controlView] mouseEntered:event]; |
| 195 } | 203 } |
| 196 | 204 |
| 197 // See comment above mouseEntered:, above. | 205 // See comment above mouseEntered:, above. |
| 198 - (void)mouseExited:(NSEvent*)event { | 206 - (void)mouseExited:(NSEvent*)event { |
| 199 [[self controlView] mouseExited:event]; | 207 [[self controlView] mouseExited:event]; |
| 200 [super mouseExited:event]; | 208 [super mouseExited:event]; |
| 201 } | 209 } |
| 202 | 210 |
| 211 - (void)setDrawFolderArrow:(BOOL)draw { |
| 212 drawFolderArrow_ = draw; |
| 213 if (draw && !arrowImage_) { |
| 214 arrowImage_.reset( |
| 215 [gfx::GetCachedImageWithName(@"menu_hierarchy_arrow.pdf") retain]); |
| 216 } |
| 217 } |
| 218 |
| 219 // Add extra size for the arrow so it doesn't overlap the text. |
| 220 // Does not sanity check to be sure this is actually a folder node. |
| 221 - (NSSize)cellSize { |
| 222 NSSize cellSize = [super cellSize]; |
| 223 if (drawFolderArrow_) { |
| 224 cellSize.width += [arrowImage_ size].width; // plus margin? |
| 225 } |
| 226 return cellSize; |
| 227 } |
| 228 |
| 229 // Override cell drawing to add a submenu arrow like a real menu. |
| 230 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 231 // First draw "everything else". |
| 232 [super drawInteriorWithFrame:cellFrame inView:controlView]; |
| 233 |
| 234 // If asked to do so, and if a folder, draw the arrow. |
| 235 if (!drawFolderArrow_) |
| 236 return; |
| 237 BookmarkButton* button = static_cast<BookmarkButton*>([self controlView]); |
| 238 DCHECK([button respondsToSelector:@selector(isFolder)]); |
| 239 if ([button isFolder]) { |
| 240 NSRect imageRect = NSZeroRect; |
| 241 imageRect.size = [arrowImage_ size]; |
| 242 const CGFloat kArrowOffset = 1.0; // Required for proper centering. |
| 243 CGFloat dX = NSWidth(cellFrame) - NSWidth(imageRect); |
| 244 CGFloat dY = (NSHeight(cellFrame) / 2.0) - (NSHeight(imageRect) / 2.0) + |
| 245 kArrowOffset; |
| 246 NSRect drawRect = NSOffsetRect(imageRect, dX, dY); |
| 247 [arrowImage_ drawInRect:drawRect |
| 248 fromRect:imageRect |
| 249 operation:NSCompositeSourceOver |
| 250 fraction:[self isEnabled] ? 1.0 : 0.5 |
| 251 neverFlipped:YES]; |
| 252 } |
| 253 } |
| 254 |
| 203 @end | 255 @end |
| OLD | NEW |