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 #include "ui/gfx/mac/nsimage_cache.h" |
17 | 17 |
18 | 18 |
19 @interface BookmarkButtonCell(Private) | 19 @interface BookmarkButtonCell(Private) |
20 - (void)configureBookmarkButtonCell; | 20 - (void)configureBookmarkButtonCell; |
| 21 - (void)applyTextColor; |
21 @end | 22 @end |
22 | 23 |
23 | 24 |
24 @implementation BookmarkButtonCell | 25 @implementation BookmarkButtonCell |
25 | 26 |
26 @synthesize startingChildIndex = startingChildIndex_; | 27 @synthesize startingChildIndex = startingChildIndex_; |
27 @synthesize drawFolderArrow = drawFolderArrow_; | 28 @synthesize drawFolderArrow = drawFolderArrow_; |
28 | 29 |
29 + (id)buttonCellForNode:(const BookmarkNode*)node | 30 + (id)buttonCellForNode:(const BookmarkNode*)node |
30 contextMenu:(NSMenu*)contextMenu | 31 contextMenu:(NSMenu*)contextMenu |
31 cellText:(NSString*)cellText | 32 cellText:(NSString*)cellText |
32 cellImage:(NSImage*)cellImage { | 33 cellImage:(NSImage*)cellImage { |
33 id buttonCell = | 34 id buttonCell = |
34 [[[BookmarkButtonCell alloc] initForNode:node | 35 [[[BookmarkButtonCell alloc] initForNode:node |
35 contextMenu:contextMenu | 36 contextMenu:contextMenu |
36 cellText:cellText | 37 cellText:cellText |
37 cellImage:cellImage] | 38 cellImage:cellImage] |
38 autorelease]; | 39 autorelease]; |
39 return buttonCell; | 40 return buttonCell; |
40 } | 41 } |
41 | 42 |
42 - (id)initForNode:(const BookmarkNode*)node | 43 - (id)initForNode:(const BookmarkNode*)node |
43 contextMenu:(NSMenu*)contextMenu | 44 contextMenu:(NSMenu*)contextMenu |
44 cellText:(NSString*)cellText | 45 cellText:(NSString*)cellText |
45 cellImage:(NSImage*)cellImage { | 46 cellImage:(NSImage*)cellImage { |
46 if ((self = [super initTextCell:cellText])) { | 47 if ((self = [super initTextCell:cellText])) { |
47 [self configureBookmarkButtonCell]; | 48 [self configureBookmarkButtonCell]; |
48 | 49 [self setTextColor:[NSColor redColor]]; |
49 [self setBookmarkNode:node]; | 50 [self setBookmarkNode:node]; |
50 | 51 |
51 if (node) { | 52 if (node) { |
52 NSString* title = base::SysUTF16ToNSString(node->GetTitle()); | 53 NSString* title = base::SysUTF16ToNSString(node->GetTitle()); |
53 [self setBookmarkCellText:title image:cellImage]; | 54 [self setBookmarkCellText:title image:cellImage]; |
54 [self setMenu:contextMenu]; | 55 [self setMenu:contextMenu]; |
55 } else { | 56 } else { |
56 [self setEmpty:YES]; | 57 [self setEmpty:YES]; |
57 [self setBookmarkCellText:l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU) | 58 [self setBookmarkCellText:l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU) |
58 image:nil]; | 59 image:nil]; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 UserMetrics::RecordAction(UserMetricsAction("BookmarkBarFolder_CtxMenu")); | 154 UserMetrics::RecordAction(UserMetricsAction("BookmarkBarFolder_CtxMenu")); |
154 } else { | 155 } else { |
155 UserMetrics::RecordAction(UserMetricsAction("BookmarkBar_CtxMenu")); | 156 UserMetrics::RecordAction(UserMetricsAction("BookmarkBar_CtxMenu")); |
156 } | 157 } |
157 | 158 |
158 [menu setRepresentedObject:[NSNumber numberWithLongLong:node->id()]]; | 159 [menu setRepresentedObject:[NSNumber numberWithLongLong:node->id()]]; |
159 | 160 |
160 return menu; | 161 return menu; |
161 } | 162 } |
162 | 163 |
163 // Unfortunately, NSCell doesn't already have something like this. | 164 - (void)setTitle:(NSString*)title { |
164 // TODO(jrg): consider placing in GTM. | 165 if ([[self title] isEqualTo:title]) |
| 166 return; |
| 167 [super setTitle:title]; |
| 168 [self applyTextColor]; |
| 169 } |
| 170 |
165 - (void)setTextColor:(NSColor*)color { | 171 - (void)setTextColor:(NSColor*)color { |
| 172 if ([textColor_ isEqualTo:color]) |
| 173 return; |
| 174 textColor_.reset([color copy]); |
| 175 [self applyTextColor]; |
| 176 } |
166 | 177 |
167 // We can't properly set the cell's text color without a control. | 178 // We must reapply the text color after any setTitle: call |
168 // In theory we could just save the next for later and wait until | 179 - (void)applyTextColor { |
169 // the cell is moved to a control, but there is no obvious way to | |
170 // accomplish that (e.g. no "cellDidMoveToControl" notification.) | |
171 DCHECK([self controlView]); | |
172 | |
173 scoped_nsobject<NSMutableParagraphStyle> style([NSMutableParagraphStyle new]); | 180 scoped_nsobject<NSMutableParagraphStyle> style([NSMutableParagraphStyle new]); |
174 [style setAlignment:NSLeftTextAlignment]; | 181 [style setAlignment:NSLeftTextAlignment]; |
175 NSDictionary* dict = [NSDictionary | 182 NSDictionary* dict = [NSDictionary |
176 dictionaryWithObjectsAndKeys:color, | 183 dictionaryWithObjectsAndKeys:textColor_, |
177 NSForegroundColorAttributeName, | 184 NSForegroundColorAttributeName, |
178 [self font], NSFontAttributeName, | 185 [self font], NSFontAttributeName, |
179 style.get(), NSParagraphStyleAttributeName, | 186 style.get(), NSParagraphStyleAttributeName, |
180 nil]; | 187 nil]; |
181 scoped_nsobject<NSAttributedString> ats([[NSAttributedString alloc] | 188 scoped_nsobject<NSAttributedString> ats([[NSAttributedString alloc] |
182 initWithString:[self title] | 189 initWithString:[self title] |
183 attributes:dict]); | 190 attributes:dict]); |
184 NSButton* button = static_cast<NSButton*>([self controlView]); | 191 [self setAttributedTitle:ats.get()]; |
185 if (button) { | |
186 DCHECK([button isKindOfClass:[NSButton class]]); | |
187 [button setAttributedTitle:ats.get()]; | |
188 } | |
189 } | 192 } |
190 | 193 |
191 // To implement "hover open a bookmark button to open the folder" | 194 // To implement "hover open a bookmark button to open the folder" |
192 // which feels like menus, we override NSButtonCell's mouseEntered: | 195 // which feels like menus, we override NSButtonCell's mouseEntered: |
193 // and mouseExited:, then and pass them along to our owning control. | 196 // and mouseExited:, then and pass them along to our owning control. |
194 // Note: as verified in a debugger, mouseEntered: does NOT increase | 197 // Note: as verified in a debugger, mouseEntered: does NOT increase |
195 // the retainCount of the cell or its owning control. | 198 // the retainCount of the cell or its owning control. |
196 - (void)mouseEntered:(NSEvent*)event { | 199 - (void)mouseEntered:(NSEvent*)event { |
197 [super mouseEntered:event]; | 200 [super mouseEntered:event]; |
198 [[self controlView] mouseEntered:event]; | 201 [[self controlView] mouseEntered:event]; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 NSRect drawRect = NSOffsetRect(imageRect, dX, dY); | 245 NSRect drawRect = NSOffsetRect(imageRect, dX, dY); |
243 [arrowImage_ drawInRect:drawRect | 246 [arrowImage_ drawInRect:drawRect |
244 fromRect:imageRect | 247 fromRect:imageRect |
245 operation:NSCompositeSourceOver | 248 operation:NSCompositeSourceOver |
246 fraction:[self isEnabled] ? 1.0 : 0.5 | 249 fraction:[self isEnabled] ? 1.0 : 0.5 |
247 neverFlipped:YES]; | 250 neverFlipped:YES]; |
248 } | 251 } |
249 } | 252 } |
250 | 253 |
251 @end | 254 @end |
OLD | NEW |