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/toolbar_controller.h" | 5 #import "chrome/browser/cocoa/toolbar_controller.h" |
6 | 6 |
| 7 #include "app/l10n_util_mac.h" |
7 #include "base/mac_util.h" | 8 #include "base/mac_util.h" |
8 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
9 #include "base/gfx/rect.h" | 10 #include "base/gfx/rect.h" |
10 #include "chrome/app/chrome_dll_resource.h" | 11 #include "chrome/app/chrome_dll_resource.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" |
11 #include "chrome/browser/autocomplete/autocomplete_popup_view.h" | 13 #include "chrome/browser/autocomplete/autocomplete_popup_view.h" |
12 #import "chrome/browser/cocoa/autocomplete_text_field.h" | 14 #import "chrome/browser/cocoa/autocomplete_text_field.h" |
13 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" | 15 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" |
14 #import "chrome/browser/cocoa/back_forward_menu_controller.h" | 16 #import "chrome/browser/cocoa/back_forward_menu_controller.h" |
15 #import "chrome/browser/cocoa/gradient_button_cell.h" | 17 #import "chrome/browser/cocoa/gradient_button_cell.h" |
16 #import "chrome/browser/cocoa/location_bar_view_mac.h" | 18 #import "chrome/browser/cocoa/location_bar_view_mac.h" |
17 #import "chrome/browser/cocoa/menu_button.h" | 19 #import "chrome/browser/cocoa/menu_button.h" |
18 #include "chrome/browser/cocoa/nsimage_cache.h" | 20 #include "chrome/browser/cocoa/nsimage_cache.h" |
19 #include "chrome/browser/profile.h" | 21 #include "chrome/browser/profile.h" |
| 22 #include "chrome/browser/search_engines/template_url_model.h" |
20 #include "chrome/browser/toolbar_model.h" | 23 #include "chrome/browser/toolbar_model.h" |
21 #include "chrome/common/notification_details.h" | 24 #include "chrome/common/notification_details.h" |
22 #include "chrome/common/notification_observer.h" | 25 #include "chrome/common/notification_observer.h" |
23 #include "chrome/common/notification_type.h" | 26 #include "chrome/common/notification_type.h" |
24 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
25 #include "chrome/common/pref_service.h" | 28 #include "chrome/common/pref_service.h" |
| 29 #include "grit/generated_resources.h" |
26 | 30 |
27 // Name of image in the bundle for the yellow of the star icon. | 31 // Name of image in the bundle for the yellow of the star icon. |
28 static NSString* const kStarredImageName = @"starred.pdf"; | 32 static NSString* const kStarredImageName = @"starred.pdf"; |
29 | 33 |
30 // Height of the toolbar in pixels when the bookmark bar is closed. | 34 // Height of the toolbar in pixels when the bookmark bar is closed. |
31 static const float kBaseToolbarHeight = 36.0; | 35 static const float kBaseToolbarHeight = 36.0; |
32 | 36 |
33 // Overlap (in pixels) between the toolbar and the bookmark bar. | 37 // Overlap (in pixels) between the toolbar and the bookmark bar. |
34 static const float kBookmarkBarOverlap = 7.0; | 38 static const float kBookmarkBarOverlap = 7.0; |
35 | 39 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 locationBarRetainer_.reset([locationBar_ retain]); | 184 locationBarRetainer_.reset([locationBar_ retain]); |
181 trackingArea_.reset( | 185 trackingArea_.reset( |
182 [[NSTrackingArea alloc] initWithRect:NSZeroRect // Ignored | 186 [[NSTrackingArea alloc] initWithRect:NSZeroRect // Ignored |
183 options:NSTrackingMouseMoved | | 187 options:NSTrackingMouseMoved | |
184 NSTrackingInVisibleRect | | 188 NSTrackingInVisibleRect | |
185 NSTrackingMouseEnteredAndExited | | 189 NSTrackingMouseEnteredAndExited | |
186 NSTrackingActiveAlways | 190 NSTrackingActiveAlways |
187 owner:self | 191 owner:self |
188 userInfo:nil]); | 192 userInfo:nil]); |
189 [[self view] addTrackingArea:trackingArea_.get()]; | 193 [[self view] addTrackingArea:trackingArea_.get()]; |
| 194 |
| 195 // We want a dynamic tooltip on the go button, so tell the go button to ask |
| 196 // use for the tooltip |
| 197 [goButton_ addToolTipRect:[goButton_ bounds] owner:self userData:nil]; |
190 } | 198 } |
191 - (void)removeFromSuperview { | 199 - (void)removeFromSuperview { |
192 NSLog(@"remove"); | 200 NSLog(@"remove"); |
193 } | 201 } |
194 - (void)mouseExited:(NSEvent*)theEvent { | 202 - (void)mouseExited:(NSEvent*)theEvent { |
195 [[hoveredButton_ cell] setMouseInside:NO animate:YES]; | 203 [[hoveredButton_ cell] setMouseInside:NO animate:YES]; |
196 [hoveredButton_ release]; | 204 [hoveredButton_ release]; |
197 hoveredButton_ = nil; | 205 hoveredButton_ = nil; |
198 } | 206 } |
199 | 207 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 [starButton_ setEnabled:commands->IsCommandEnabled(IDC_STAR) ? YES : NO]; | 290 [starButton_ setEnabled:commands->IsCommandEnabled(IDC_STAR) ? YES : NO]; |
283 } | 291 } |
284 | 292 |
285 - (void)updateToolbarWithContents:(TabContents*)tab | 293 - (void)updateToolbarWithContents:(TabContents*)tab |
286 shouldRestoreState:(BOOL)shouldRestore { | 294 shouldRestoreState:(BOOL)shouldRestore { |
287 locationBarView_->Update(tab, shouldRestore ? true : false); | 295 locationBarView_->Update(tab, shouldRestore ? true : false); |
288 } | 296 } |
289 | 297 |
290 - (void)setStarredState:(BOOL)isStarred { | 298 - (void)setStarredState:(BOOL)isStarred { |
291 NSImage* starImage = nil; | 299 NSImage* starImage = nil; |
292 if (isStarred) | 300 NSString* toolTip; |
| 301 if (isStarred) { |
293 starImage = nsimage_cache::ImageNamed(kStarredImageName); | 302 starImage = nsimage_cache::ImageNamed(kStarredImageName); |
| 303 // Cache the string since we'll need it a lot |
| 304 static NSString* starredToolTip = |
| 305 [l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_STARRED) retain]; |
| 306 toolTip = starredToolTip; |
| 307 } else { |
| 308 // Cache the string since we'll need it a lot |
| 309 static NSString* starToolTip = |
| 310 [l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_STAR) retain]; |
| 311 toolTip = starToolTip; |
| 312 } |
294 | 313 |
295 [(GradientButtonCell*)[starButton_ cell] setUnderlayImage:starImage]; | 314 [(GradientButtonCell*)[starButton_ cell] setUnderlayImage:starImage]; |
| 315 [starButton_ setToolTip:toolTip]; |
296 } | 316 } |
297 | 317 |
298 - (void)setIsLoading:(BOOL)isLoading { | 318 - (void)setIsLoading:(BOOL)isLoading { |
299 NSString* imageName = @"go_Template.pdf"; | 319 NSString* imageName = @"go_Template.pdf"; |
300 NSInteger tag = IDC_GO; | 320 NSInteger tag = IDC_GO; |
301 if (isLoading) { | 321 if (isLoading) { |
302 imageName = @"stop_Template.pdf"; | 322 imageName = @"stop_Template.pdf"; |
303 tag = IDC_STOP; | 323 tag = IDC_STOP; |
304 } | 324 } |
305 NSImage* stopStartImage = nsimage_cache::ImageNamed(imageName); | 325 NSImage* stopStartImage = nsimage_cache::ImageNamed(imageName); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 } else if (*prefName == prefs::kShowPageOptionsButtons) { | 457 } else if (*prefName == prefs::kShowPageOptionsButtons) { |
438 [self showOptionalPageWrenchButtons]; | 458 [self showOptionalPageWrenchButtons]; |
439 } | 459 } |
440 } | 460 } |
441 | 461 |
442 - (NSRect)starButtonInWindowCoordinates { | 462 - (NSRect)starButtonInWindowCoordinates { |
443 return [[[starButton_ window] contentView] convertRect:[starButton_ bounds] | 463 return [[[starButton_ window] contentView] convertRect:[starButton_ bounds] |
444 fromView:starButton_]; | 464 fromView:starButton_]; |
445 } | 465 } |
446 | 466 |
| 467 - (NSString *)view:(NSView *)view |
| 468 stringForToolTip:(NSToolTipTag)tag |
| 469 point:(NSPoint)point |
| 470 userData:(void *)userData { |
| 471 DCHECK(view == goButton_); |
| 472 |
| 473 // Following chrome/browser/views/go_button.cc: GoButton::GetTooltipText() |
| 474 |
| 475 // Is it currently 'stop'? |
| 476 if ([goButton_ tag] == IDC_STOP) { |
| 477 return l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_STOP); |
| 478 } |
| 479 |
| 480 // It is 'go', so see what it would do... |
| 481 |
| 482 // Fetch the EditView and EditModel |
| 483 LocationBar* locationBar = [self locationBar]; |
| 484 DCHECK(locationBar); |
| 485 AutocompleteEditView* editView = locationBar->location_entry(); |
| 486 DCHECK(editView); |
| 487 AutocompleteEditModel* editModel = editView->model(); |
| 488 DCHECK(editModel); |
| 489 |
| 490 std::wstring currentText(editView->GetText()); |
| 491 if (currentText.empty()) { |
| 492 return nil; |
| 493 } |
| 494 string16 currentText16(WideToUTF16Hack(currentText)); |
| 495 |
| 496 // It is simply an url it is gonna go to, build the tip with the info. |
| 497 if (editModel->CurrentTextIsURL()) { |
| 498 return l10n_util::GetNSStringF(IDS_TOOLTIP_GO_SITE, currentText16); |
| 499 } |
| 500 |
| 501 // Build the tip based on what provide/template it will get. |
| 502 std::wstring keyword(editModel->keyword()); |
| 503 TemplateURLModel* template_url_model = |
| 504 editModel->profile()->GetTemplateURLModel(); |
| 505 const TemplateURL* provider = |
| 506 (keyword.empty() || editModel->is_keyword_hint()) ? |
| 507 template_url_model->GetDefaultSearchProvider() : |
| 508 template_url_model->GetTemplateURLForKeyword(keyword); |
| 509 if (!provider) |
| 510 return nil; |
| 511 std::wstring shortName(provider->AdjustedShortNameForLocaleDirection()); |
| 512 return l10n_util::GetNSStringF(IDS_TOOLTIP_GO_SEARCH, |
| 513 WideToUTF16(shortName), currentText16); |
| 514 |
| 515 } |
| 516 |
447 - (gfx::Rect)autocompletePopupPosition { | 517 - (gfx::Rect)autocompletePopupPosition { |
448 // The popup should span from the left edge of the star button to the right | 518 // The popup should span from the left edge of the star button to the right |
449 // edge of the go button. The returned height is ignored. | 519 // edge of the go button. The returned height is ignored. |
450 NSRect locationFrame = [locationBar_ frame]; | 520 NSRect locationFrame = [locationBar_ frame]; |
451 // TODO(shess): The buttons have an extra 2 pixels between the edge | 521 // TODO(shess): The buttons have an extra 2 pixels between the edge |
452 // of the visual button and the edge of the logical button. This | 522 // of the visual button and the edge of the logical button. This |
453 // seems wrong. | 523 // seems wrong. |
454 int minX = NSMinX([starButton_ frame]) + 2.0; | 524 int minX = NSMinX([starButton_ frame]) + 2.0; |
455 int maxX = NSMaxX([goButton_ frame]) - 2.0; | 525 int maxX = NSMaxX([goButton_ frame]) - 2.0; |
456 DCHECK(minX < NSMinX(locationFrame)); | 526 DCHECK(minX < NSMinX(locationFrame)); |
457 DCHECK(maxX > NSMaxX(locationFrame)); | 527 DCHECK(maxX > NSMaxX(locationFrame)); |
458 | 528 |
459 NSRect r = NSMakeRect(minX, NSMinY(locationFrame), maxX - minX, 0); | 529 NSRect r = NSMakeRect(minX, NSMinY(locationFrame), maxX - minX, 0); |
460 return gfx::Rect(NSRectToCGRect([[self view] convertRect:r toView:nil])); | 530 return gfx::Rect(NSRectToCGRect([[self view] convertRect:r toView:nil])); |
461 } | 531 } |
462 @end | 532 @end |
OLD | NEW |