OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/location_bar/location_bar_view_mac.h" | 5 #import "chrome/browser/cocoa/location_bar/location_bar_view_mac.h" |
6 | 6 |
7 #include "app/l10n_util_mac.h" | 7 #include "app/l10n_util_mac.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
10 #include "base/nsimage_cache_mac.h" | 10 #include "base/nsimage_cache_mac.h" |
11 #include "base/stl_util-inl.h" | 11 #include "base/stl_util-inl.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
14 #include "chrome/app/chrome_dll_resource.h" | 14 #include "chrome/app/chrome_dll_resource.h" |
15 #include "chrome/browser/alternate_nav_url_fetcher.h" | 15 #include "chrome/browser/alternate_nav_url_fetcher.h" |
16 #import "chrome/browser/app_controller_mac.h" | 16 #import "chrome/browser/app_controller_mac.h" |
17 #import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" | 17 #import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" |
18 #include "chrome/browser/browser_list.h" | 18 #include "chrome/browser/browser_list.h" |
19 #import "chrome/browser/cocoa/content_blocked_bubble_controller.h" | 19 #import "chrome/browser/cocoa/content_blocked_bubble_controller.h" |
20 #include "chrome/browser/cocoa/event_utils.h" | 20 #include "chrome/browser/cocoa/event_utils.h" |
21 #import "chrome/browser/cocoa/extensions/extension_action_context_menu.h" | 21 #import "chrome/browser/cocoa/extensions/extension_action_context_menu.h" |
22 #import "chrome/browser/cocoa/extensions/extension_popup_controller.h" | 22 #import "chrome/browser/cocoa/extensions/extension_popup_controller.h" |
23 #import "chrome/browser/cocoa/first_run_bubble_controller.h" | 23 #import "chrome/browser/cocoa/first_run_bubble_controller.h" |
24 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field.h" | 24 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field.h" |
25 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h" | 25 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h" |
| 26 #import "chrome/browser/cocoa/location_bar/content_setting_decoration.h" |
26 #import "chrome/browser/cocoa/location_bar/ev_bubble_decoration.h" | 27 #import "chrome/browser/cocoa/location_bar/ev_bubble_decoration.h" |
27 #import "chrome/browser/cocoa/location_bar/location_icon_decoration.h" | 28 #import "chrome/browser/cocoa/location_bar/location_icon_decoration.h" |
28 #import "chrome/browser/cocoa/location_bar/page_action_decoration.h" | 29 #import "chrome/browser/cocoa/location_bar/page_action_decoration.h" |
29 #import "chrome/browser/cocoa/location_bar/selected_keyword_decoration.h" | 30 #import "chrome/browser/cocoa/location_bar/selected_keyword_decoration.h" |
30 #import "chrome/browser/cocoa/location_bar/star_decoration.h" | 31 #import "chrome/browser/cocoa/location_bar/star_decoration.h" |
31 #include "chrome/browser/command_updater.h" | 32 #include "chrome/browser/command_updater.h" |
32 #include "chrome/browser/content_setting_image_model.h" | 33 #include "chrome/browser/content_setting_image_model.h" |
33 #include "chrome/browser/content_setting_bubble_model.h" | 34 #include "chrome/browser/content_setting_bubble_model.h" |
34 #include "chrome/browser/extensions/extension_browser_event_router.h" | 35 #include "chrome/browser/extensions/extension_browser_event_router.h" |
35 #include "chrome/browser/extensions/extensions_service.h" | 36 #include "chrome/browser/extensions/extensions_service.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 new SelectedKeywordDecoration([field_ font])), | 71 new SelectedKeywordDecoration([field_ font])), |
71 ev_bubble_decoration_( | 72 ev_bubble_decoration_( |
72 new EVBubbleDecoration(location_icon_decoration_.get(), | 73 new EVBubbleDecoration(location_icon_decoration_.get(), |
73 [field_ font])), | 74 [field_ font])), |
74 star_decoration_(new StarDecoration(command_updater)), | 75 star_decoration_(new StarDecoration(command_updater)), |
75 profile_(profile), | 76 profile_(profile), |
76 browser_(browser), | 77 browser_(browser), |
77 toolbar_model_(toolbar_model), | 78 toolbar_model_(toolbar_model), |
78 transition_(PageTransition::TYPED), | 79 transition_(PageTransition::TYPED), |
79 first_run_bubble_(this) { | 80 first_run_bubble_(this) { |
80 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 81 for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
81 ContentSettingImageView* content_setting_view = | 82 DCHECK_EQ(i, content_setting_decorations_.size()); |
82 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, | 83 ContentSettingsType type = static_cast<ContentSettingsType>(i); |
83 profile_); | 84 content_setting_decorations_.push_back( |
84 content_setting_views_.push_back(content_setting_view); | 85 new ContentSettingDecoration(type, this, profile_)); |
85 content_setting_view->SetVisible(false); | |
86 } | 86 } |
87 | 87 |
88 AutocompleteTextFieldCell* cell = [field_ cell]; | |
89 [cell setContentSettingViewsList:&content_setting_views_]; | |
90 | |
91 registrar_.Add(this, | 88 registrar_.Add(this, |
92 NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, | 89 NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, |
93 NotificationService::AllSources()); | 90 NotificationService::AllSources()); |
94 } | 91 } |
95 | 92 |
96 LocationBarViewMac::~LocationBarViewMac() { | 93 LocationBarViewMac::~LocationBarViewMac() { |
97 // Disconnect from cell in case it outlives us. | 94 // Disconnect from cell in case it outlives us. |
98 [[field_ cell] clearDecorations]; | 95 [[field_ cell] clearDecorations]; |
99 } | 96 } |
100 | 97 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 134 |
138 void LocationBarViewMac::FocusLocation(bool select_all) { | 135 void LocationBarViewMac::FocusLocation(bool select_all) { |
139 edit_view_->FocusLocation(select_all); | 136 edit_view_->FocusLocation(select_all); |
140 } | 137 } |
141 | 138 |
142 void LocationBarViewMac::FocusSearch() { | 139 void LocationBarViewMac::FocusSearch() { |
143 edit_view_->SetForcedQuery(); | 140 edit_view_->SetForcedQuery(); |
144 } | 141 } |
145 | 142 |
146 void LocationBarViewMac::UpdateContentSettingsIcons() { | 143 void LocationBarViewMac::UpdateContentSettingsIcons() { |
147 RefreshContentSettingsViews(); | 144 RefreshContentSettingsDecorations(); |
148 [field_ updateCursorAndToolTipRects]; | 145 [field_ updateCursorAndToolTipRects]; |
149 [field_ setNeedsDisplay:YES]; | 146 [field_ setNeedsDisplay:YES]; |
150 } | 147 } |
151 | 148 |
152 void LocationBarViewMac::UpdatePageActions() { | 149 void LocationBarViewMac::UpdatePageActions() { |
153 size_t count_before = page_action_decorations_.size(); | 150 size_t count_before = page_action_decorations_.size(); |
154 RefreshPageActionDecorations(); | 151 RefreshPageActionDecorations(); |
155 Layout(); | 152 Layout(); |
156 if (page_action_decorations_.size() != count_before) { | 153 if (page_action_decorations_.size() != count_before) { |
157 NotificationService::current()->Notify( | 154 NotificationService::current()->Notify( |
(...skipping 16 matching lines...) Expand all Loading... |
174 } | 171 } |
175 | 172 |
176 void LocationBarViewMac::SaveStateToContents(TabContents* contents) { | 173 void LocationBarViewMac::SaveStateToContents(TabContents* contents) { |
177 // TODO(shess): Why SaveStateToContents vs SaveStateToTab? | 174 // TODO(shess): Why SaveStateToContents vs SaveStateToTab? |
178 edit_view_->SaveStateToTab(contents); | 175 edit_view_->SaveStateToTab(contents); |
179 } | 176 } |
180 | 177 |
181 void LocationBarViewMac::Update(const TabContents* contents, | 178 void LocationBarViewMac::Update(const TabContents* contents, |
182 bool should_restore_state) { | 179 bool should_restore_state) { |
183 RefreshPageActionDecorations(); | 180 RefreshPageActionDecorations(); |
184 RefreshContentSettingsViews(); | 181 RefreshContentSettingsDecorations(); |
185 // AutocompleteEditView restores state if the tab is non-NULL. | 182 // AutocompleteEditView restores state if the tab is non-NULL. |
186 edit_view_->Update(should_restore_state ? contents : NULL); | 183 edit_view_->Update(should_restore_state ? contents : NULL); |
187 OnChanged(); | 184 OnChanged(); |
188 } | 185 } |
189 | 186 |
190 void LocationBarViewMac::OnAutocompleteAccept(const GURL& url, | 187 void LocationBarViewMac::OnAutocompleteAccept(const GURL& url, |
191 WindowOpenDisposition disposition, | 188 WindowOpenDisposition disposition, |
192 PageTransition::Type transition, | 189 PageTransition::Type transition, |
193 const GURL& alternate_nav_url) { | 190 const GURL& alternate_nav_url) { |
194 if (!url.is_valid()) | 191 if (!url.is_valid()) |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 NOTREACHED() << "Unexpected notification"; | 417 NOTREACHED() << "Unexpected notification"; |
421 break; | 418 break; |
422 } | 419 } |
423 } | 420 } |
424 | 421 |
425 void LocationBarViewMac::PostNotification(NSString* notification) { | 422 void LocationBarViewMac::PostNotification(NSString* notification) { |
426 [[NSNotificationCenter defaultCenter] postNotificationName:notification | 423 [[NSNotificationCenter defaultCenter] postNotificationName:notification |
427 object:[NSValue valueWithPointer:this]]; | 424 object:[NSValue valueWithPointer:this]]; |
428 } | 425 } |
429 | 426 |
430 void LocationBarViewMac::RefreshContentSettingsViews() { | 427 void LocationBarViewMac::RefreshContentSettingsDecorations() { |
431 const TabContents* tab_contents = browser_->GetSelectedTabContents(); | 428 const bool input_in_progress = toolbar_model_->input_in_progress(); |
432 for (ContentSettingViews::iterator it(content_setting_views_.begin()); | 429 const TabContents* tab_contents = |
433 it != content_setting_views_.end(); | 430 input_in_progress ? NULL : browser_->GetSelectedTabContents(); |
434 ++it) { | 431 for (size_t i = 0; i < content_setting_decorations_.size(); ++i) { |
435 (*it)->UpdateFromTabContents( | 432 content_setting_decorations_[i]->UpdateFromTabContents(tab_contents); |
436 toolbar_model_->input_in_progress() ? NULL : tab_contents); | |
437 } | 433 } |
438 } | 434 } |
439 | 435 |
440 // LocationBarImageView--------------------------------------------------------- | |
441 | |
442 void LocationBarViewMac::LocationBarImageView::SetImage(NSImage* image) { | |
443 image_.reset([image retain]); | |
444 } | |
445 | |
446 void LocationBarViewMac::LocationBarImageView::SetIcon(int resource_id) { | |
447 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
448 SetImage(rb.GetNSImageNamed(resource_id)); | |
449 } | |
450 | |
451 void LocationBarViewMac::LocationBarImageView::SetLabel(NSString* text, | |
452 NSFont* baseFont, | |
453 NSColor* color) { | |
454 // Create an attributed string for the label, if a label was given. | |
455 label_.reset(); | |
456 if (text) { | |
457 DCHECK(color); | |
458 DCHECK(baseFont); | |
459 NSFont* font = [NSFont fontWithDescriptor:[baseFont fontDescriptor] | |
460 size:[baseFont pointSize] - 2.0]; | |
461 NSDictionary* attributes = | |
462 [NSDictionary dictionaryWithObjectsAndKeys: | |
463 color, NSForegroundColorAttributeName, | |
464 font, NSFontAttributeName, | |
465 NULL]; | |
466 NSAttributedString* attrStr = | |
467 [[NSAttributedString alloc] initWithString:text attributes:attributes]; | |
468 label_.reset(attrStr); | |
469 } | |
470 } | |
471 | |
472 void LocationBarViewMac::LocationBarImageView::SetVisible(bool visible) { | |
473 visible_ = visible; | |
474 } | |
475 | |
476 NSSize LocationBarViewMac::LocationBarImageView::GetDefaultImageSize() const { | |
477 return NSZeroSize; | |
478 } | |
479 | |
480 NSSize LocationBarViewMac::LocationBarImageView::GetImageSize() const { | |
481 NSImage* image = GetImage(); | |
482 if (image) | |
483 return [image size]; | |
484 return GetDefaultImageSize(); | |
485 } | |
486 | |
487 // ContentSettingsImageView----------------------------------------------------- | |
488 LocationBarViewMac::ContentSettingImageView::ContentSettingImageView( | |
489 ContentSettingsType settings_type, | |
490 LocationBarViewMac* owner, | |
491 Profile* profile) | |
492 : content_setting_image_model_( | |
493 ContentSettingImageModel::CreateContentSettingImageModel( | |
494 settings_type)), | |
495 owner_(owner), | |
496 profile_(profile) { | |
497 } | |
498 | |
499 LocationBarViewMac::ContentSettingImageView::~ContentSettingImageView() {} | |
500 | |
501 void LocationBarViewMac::ContentSettingImageView::OnMousePressed(NSRect bounds) | |
502 { | |
503 // Get host. This should be shared shared on linux/win/osx medium-term. | |
504 TabContents* tabContents = | |
505 BrowserList::GetLastActive()->GetSelectedTabContents(); | |
506 if (!tabContents) | |
507 return; | |
508 GURL url = tabContents->GetURL(); | |
509 std::wstring displayHost; | |
510 net::AppendFormattedHost( | |
511 url, | |
512 UTF8ToWide(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), | |
513 &displayHost, NULL, NULL); | |
514 | |
515 // Transform mouse coordinates to screen space. | |
516 AutocompleteTextField* textField = owner_->GetAutocompleteTextField(); | |
517 NSWindow* window = [textField window]; | |
518 bounds = [textField convertRect:bounds toView:nil]; | |
519 NSPoint anchor = NSMakePoint(NSMidX(bounds) + 1, NSMinY(bounds)); | |
520 anchor = [window convertBaseToScreen:anchor]; | |
521 | |
522 // Open bubble. | |
523 ContentSettingBubbleModel* model = | |
524 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | |
525 tabContents, profile_, | |
526 content_setting_image_model_->get_content_settings_type()); | |
527 [ContentBlockedBubbleController showForModel:model | |
528 parentWindow:window | |
529 anchoredAt:anchor]; | |
530 } | |
531 | |
532 NSString* LocationBarViewMac::ContentSettingImageView::GetToolTip() { | |
533 return tooltip_.get(); | |
534 } | |
535 | |
536 void LocationBarViewMac::ContentSettingImageView::UpdateFromTabContents( | |
537 const TabContents* tab_contents) { | |
538 content_setting_image_model_->UpdateFromTabContents(tab_contents); | |
539 if (content_setting_image_model_->is_visible()) { | |
540 // TODO(thakis): We should use pdfs for these icons on OSX. | |
541 // http://crbug.com/35847 | |
542 SetIcon(content_setting_image_model_->get_icon()); | |
543 SetToolTip(base::SysUTF8ToNSString( | |
544 content_setting_image_model_->get_tooltip())); | |
545 SetVisible(true); | |
546 } else { | |
547 SetVisible(false); | |
548 } | |
549 } | |
550 | |
551 void LocationBarViewMac::ContentSettingImageView::SetToolTip(NSString* tooltip) | |
552 { | |
553 tooltip_.reset([tooltip retain]); | |
554 } | |
555 | |
556 void LocationBarViewMac::DeletePageActionDecorations() { | 436 void LocationBarViewMac::DeletePageActionDecorations() { |
557 // TODO(shess): Deleting these decorations could result in the cell | 437 // TODO(shess): Deleting these decorations could result in the cell |
558 // refering to them before things are laid out again. Meanwhile, at | 438 // refering to them before things are laid out again. Meanwhile, at |
559 // least fail safe. | 439 // least fail safe. |
560 [[field_ cell] clearDecorations]; | 440 [[field_ cell] clearDecorations]; |
561 | 441 |
562 page_action_decorations_.reset(); | 442 page_action_decorations_.reset(); |
563 } | 443 } |
564 | 444 |
565 void LocationBarViewMac::RefreshPageActionDecorations() { | 445 void LocationBarViewMac::RefreshPageActionDecorations() { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 // Reset the left-hand decorations. | 489 // Reset the left-hand decorations. |
610 // TODO(shess): Shortly, this code will live somewhere else, like in | 490 // TODO(shess): Shortly, this code will live somewhere else, like in |
611 // the constructor. I am still wrestling with how best to deal with | 491 // the constructor. I am still wrestling with how best to deal with |
612 // right-hand decorations, which are not a static set. | 492 // right-hand decorations, which are not a static set. |
613 [cell clearDecorations]; | 493 [cell clearDecorations]; |
614 [cell addLeftDecoration:location_icon_decoration_.get()]; | 494 [cell addLeftDecoration:location_icon_decoration_.get()]; |
615 [cell addLeftDecoration:selected_keyword_decoration_.get()]; | 495 [cell addLeftDecoration:selected_keyword_decoration_.get()]; |
616 [cell addLeftDecoration:ev_bubble_decoration_.get()]; | 496 [cell addLeftDecoration:ev_bubble_decoration_.get()]; |
617 [cell addRightDecoration:star_decoration_.get()]; | 497 [cell addRightDecoration:star_decoration_.get()]; |
618 | 498 |
619 // Display order is right to left. | 499 // Note that display order is right to left. |
620 for (size_t i = 0; i < page_action_decorations_.size(); ++i) { | 500 for (size_t i = 0; i < page_action_decorations_.size(); ++i) { |
621 [cell addRightDecoration:page_action_decorations_[i]]; | 501 [cell addRightDecoration:page_action_decorations_[i]]; |
622 } | 502 } |
| 503 for (size_t i = 0; i < content_setting_decorations_.size(); ++i) { |
| 504 [cell addRightDecoration:content_setting_decorations_[i]]; |
| 505 } |
623 | 506 |
624 // By default only the location icon is visible. | 507 // By default only the location icon is visible. |
625 location_icon_decoration_->SetVisible(true); | 508 location_icon_decoration_->SetVisible(true); |
626 selected_keyword_decoration_->SetVisible(false); | 509 selected_keyword_decoration_->SetVisible(false); |
627 ev_bubble_decoration_->SetVisible(false); | 510 ev_bubble_decoration_->SetVisible(false); |
628 | 511 |
629 // Get the keyword to use for keyword-search and hinting. | 512 // Get the keyword to use for keyword-search and hinting. |
630 const std::wstring keyword(edit_view_->model()->keyword()); | 513 const std::wstring keyword(edit_view_->model()->keyword()); |
631 std::wstring short_name; | 514 std::wstring short_name; |
632 bool is_extension_keyword = false; | 515 bool is_extension_keyword = false; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 | 572 |
690 // These need to change anytime the layout changes. | 573 // These need to change anytime the layout changes. |
691 // TODO(shess): Anytime the field editor might have changed, the | 574 // TODO(shess): Anytime the field editor might have changed, the |
692 // cursor rects almost certainly should have changed. The tooltips | 575 // cursor rects almost certainly should have changed. The tooltips |
693 // might change even when the rects don't change. | 576 // might change even when the rects don't change. |
694 [field_ resetFieldEditorFrameIfNeeded]; | 577 [field_ resetFieldEditorFrameIfNeeded]; |
695 [field_ updateCursorAndToolTipRects]; | 578 [field_ updateCursorAndToolTipRects]; |
696 | 579 |
697 [field_ setNeedsDisplay:YES]; | 580 [field_ setNeedsDisplay:YES]; |
698 } | 581 } |
OLD | NEW |