| 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 #include "chrome/browser/gtk/location_bar_view_gtk.h" | 5 #include "chrome/browser/gtk/location_bar_view_gtk.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/gfx/gtk_util.h" | 12 #include "base/gfx/gtk_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "chrome/app/chrome_dll_resource.h" | 15 #include "chrome/app/chrome_dll_resource.h" |
| 16 #include "chrome/browser/alternate_nav_url_fetcher.h" | 16 #include "chrome/browser/alternate_nav_url_fetcher.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" | 17 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" |
| 18 #include "chrome/browser/browser.h" |
| 18 #include "chrome/browser/browser_list.h" | 19 #include "chrome/browser/browser_list.h" |
| 19 #include "chrome/browser/command_updater.h" | 20 #include "chrome/browser/command_updater.h" |
| 21 #include "chrome/browser/extensions/extension_browser_event_router.h" |
| 22 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 20 #include "chrome/browser/gtk/first_run_bubble.h" | 23 #include "chrome/browser/gtk/first_run_bubble.h" |
| 21 #include "chrome/browser/gtk/gtk_theme_provider.h" | 24 #include "chrome/browser/gtk/gtk_theme_provider.h" |
| 22 #include "chrome/browser/gtk/rounded_window.h" | 25 #include "chrome/browser/gtk/rounded_window.h" |
| 23 #include "chrome/browser/profile.h" | 26 #include "chrome/browser/profile.h" |
| 24 #include "chrome/browser/search_engines/template_url.h" | 27 #include "chrome/browser/search_engines/template_url.h" |
| 25 #include "chrome/browser/search_engines/template_url_model.h" | 28 #include "chrome/browser/search_engines/template_url_model.h" |
| 26 #include "chrome/browser/tab_contents/tab_contents.h" | 29 #include "chrome/browser/tab_contents/tab_contents.h" |
| 27 #include "chrome/common/gtk_util.h" | 30 #include "chrome/common/gtk_util.h" |
| 28 #include "chrome/common/notification_service.h" | 31 #include "chrome/common/notification_service.h" |
| 29 #include "chrome/common/page_transition_types.h" | 32 #include "chrome/common/page_transition_types.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // Make sure the TemplateURL still exists. | 76 // Make sure the TemplateURL still exists. |
| 74 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLModel | 77 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLModel |
| 75 // to track changes to the model, this should become a DCHECK. | 78 // to track changes to the model, this should become a DCHECK. |
| 76 const TemplateURL* template_url = | 79 const TemplateURL* template_url = |
| 77 profile->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword); | 80 profile->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword); |
| 78 if (template_url) | 81 if (template_url) |
| 79 return template_url->AdjustedShortNameForLocaleDirection(); | 82 return template_url->AdjustedShortNameForLocaleDirection(); |
| 80 return std::wstring(); | 83 return std::wstring(); |
| 81 } | 84 } |
| 82 | 85 |
| 86 // If widget is visible, increment the int pointed to by count. |
| 87 // Suitible for use with gtk_container_foreach. |
| 88 void CountVisibleWidgets(GtkWidget* widget, gpointer count) { |
| 89 if (GTK_WIDGET_VISIBLE(widget)) |
| 90 *static_cast<int*>(count) += 1; |
| 91 } |
| 92 |
| 83 } // namespace | 93 } // namespace |
| 84 | 94 |
| 95 //////////////////////////////////////////////////////////////////////////////// |
| 96 // LocationBarViewGtk |
| 97 |
| 85 // static | 98 // static |
| 86 const GdkColor LocationBarViewGtk::kBackgroundColorByLevel[3] = { | 99 const GdkColor LocationBarViewGtk::kBackgroundColorByLevel[3] = { |
| 87 GDK_COLOR_RGB(255, 245, 195), // SecurityLevel SECURE: Yellow. | 100 GDK_COLOR_RGB(255, 245, 195), // SecurityLevel SECURE: Yellow. |
| 88 GDK_COLOR_RGB(255, 255, 255), // SecurityLevel NORMAL: White. | 101 GDK_COLOR_RGB(255, 255, 255), // SecurityLevel NORMAL: White. |
| 89 GDK_COLOR_RGB(255, 255, 255), // SecurityLevel INSECURE: White. | 102 GDK_COLOR_RGB(255, 255, 255), // SecurityLevel INSECURE: White. |
| 90 }; | 103 }; |
| 91 | 104 |
| 92 LocationBarViewGtk::LocationBarViewGtk(CommandUpdater* command_updater, | 105 LocationBarViewGtk::LocationBarViewGtk(CommandUpdater* command_updater, |
| 93 ToolbarModel* toolbar_model, AutocompletePopupPositioner* popup_positioner) | 106 ToolbarModel* toolbar_model, AutocompletePopupPositioner* popup_positioner, |
| 107 Browser* browser) |
| 94 : security_icon_event_box_(NULL), | 108 : security_icon_event_box_(NULL), |
| 95 security_lock_icon_image_(NULL), | 109 security_lock_icon_image_(NULL), |
| 96 security_warning_icon_image_(NULL), | 110 security_warning_icon_image_(NULL), |
| 97 info_label_(NULL), | 111 info_label_(NULL), |
| 98 tab_to_search_box_(NULL), | 112 tab_to_search_box_(NULL), |
| 99 tab_to_search_label_(NULL), | 113 tab_to_search_label_(NULL), |
| 100 tab_to_search_hint_(NULL), | 114 tab_to_search_hint_(NULL), |
| 101 tab_to_search_hint_leading_label_(NULL), | 115 tab_to_search_hint_leading_label_(NULL), |
| 102 tab_to_search_hint_icon_(NULL), | 116 tab_to_search_hint_icon_(NULL), |
| 103 tab_to_search_hint_trailing_label_(NULL), | 117 tab_to_search_hint_trailing_label_(NULL), |
| 104 profile_(NULL), | 118 profile_(NULL), |
| 105 command_updater_(command_updater), | 119 command_updater_(command_updater), |
| 106 toolbar_model_(toolbar_model), | 120 toolbar_model_(toolbar_model), |
| 121 browser_(browser), |
| 107 popup_positioner_(popup_positioner), | 122 popup_positioner_(popup_positioner), |
| 108 disposition_(CURRENT_TAB), | 123 disposition_(CURRENT_TAB), |
| 109 transition_(PageTransition::TYPED), | 124 transition_(PageTransition::TYPED), |
| 110 first_run_bubble_(this), | 125 first_run_bubble_(this), |
| 111 popup_window_mode_(false), | 126 popup_window_mode_(false), |
| 112 theme_provider_(NULL) { | 127 theme_provider_(NULL) { |
| 113 } | 128 } |
| 114 | 129 |
| 115 LocationBarViewGtk::~LocationBarViewGtk() { | 130 LocationBarViewGtk::~LocationBarViewGtk() { |
| 116 // All of our widgets should have be children of / owned by the alignment. | 131 // All of our widgets should have be children of / owned by the alignment. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 // Make the event box not visible so it does not paint a background. | 234 // Make the event box not visible so it does not paint a background. |
| 220 gtk_event_box_set_visible_window(GTK_EVENT_BOX(security_icon_event_box_), | 235 gtk_event_box_set_visible_window(GTK_EVENT_BOX(security_icon_event_box_), |
| 221 FALSE); | 236 FALSE); |
| 222 g_signal_connect(security_icon_event_box_, "button-press-event", | 237 g_signal_connect(security_icon_event_box_, "button-press-event", |
| 223 G_CALLBACK(&OnSecurityIconPressed), this); | 238 G_CALLBACK(&OnSecurityIconPressed), this); |
| 224 | 239 |
| 225 gtk_container_add(GTK_CONTAINER(security_icon_event_box_), security_icon_box); | 240 gtk_container_add(GTK_CONTAINER(security_icon_event_box_), security_icon_box); |
| 226 gtk_box_pack_end(GTK_BOX(hbox_.get()), security_icon_event_box_, | 241 gtk_box_pack_end(GTK_BOX(hbox_.get()), security_icon_event_box_, |
| 227 FALSE, FALSE, 0); | 242 FALSE, FALSE, 0); |
| 228 | 243 |
| 244 page_action_hbox_ = gtk_hbox_new(FALSE, kInnerPadding); |
| 245 gtk_box_pack_end(GTK_BOX(hbox_.get()), page_action_hbox_, FALSE, FALSE, 0); |
| 246 |
| 229 registrar_.Add(this, | 247 registrar_.Add(this, |
| 230 NotificationType::BROWSER_THEME_CHANGED, | 248 NotificationType::BROWSER_THEME_CHANGED, |
| 231 NotificationService::AllSources()); | 249 NotificationService::AllSources()); |
| 232 theme_provider_ = GtkThemeProvider::GetFrom(profile_); | 250 theme_provider_ = GtkThemeProvider::GetFrom(profile_); |
| 233 theme_provider_->InitThemesFor(this); | 251 theme_provider_->InitThemesFor(this); |
| 234 } | 252 } |
| 235 | 253 |
| 236 void LocationBarViewGtk::SetProfile(Profile* profile) { | 254 void LocationBarViewGtk::SetProfile(Profile* profile) { |
| 237 profile_ = profile; | 255 profile_ = profile; |
| 238 } | 256 } |
| 239 | 257 |
| 240 void LocationBarViewGtk::Update(const TabContents* contents) { | 258 void LocationBarViewGtk::Update(const TabContents* contents) { |
| 241 SetSecurityIcon(toolbar_model_->GetIcon()); | 259 SetSecurityIcon(toolbar_model_->GetIcon()); |
| 260 UpdatePageActions(); |
| 242 SetInfoText(); | 261 SetInfoText(); |
| 243 location_entry_->Update(contents); | 262 location_entry_->Update(contents); |
| 244 // The security level (background color) could have changed, etc. | 263 // The security level (background color) could have changed, etc. |
| 245 if (theme_provider_->UseGtkTheme()) { | 264 if (theme_provider_->UseGtkTheme()) { |
| 246 // In GTK mode, we need our parent to redraw, as it draws the text entry | 265 // In GTK mode, we need our parent to redraw, as it draws the text entry |
| 247 // border. | 266 // border. |
| 248 gtk_widget_queue_draw(widget()->parent); | 267 gtk_widget_queue_draw(widget()->parent); |
| 249 } else { | 268 } else { |
| 250 gtk_widget_queue_draw(widget()); | 269 gtk_widget_queue_draw(widget()); |
| 251 } | 270 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 location_entry_->SetFocus(); | 381 location_entry_->SetFocus(); |
| 363 location_entry_->SelectAll(true); | 382 location_entry_->SelectAll(true); |
| 364 } | 383 } |
| 365 | 384 |
| 366 void LocationBarViewGtk::FocusSearch() { | 385 void LocationBarViewGtk::FocusSearch() { |
| 367 location_entry_->SetFocus(); | 386 location_entry_->SetFocus(); |
| 368 location_entry_->SetForcedQuery(); | 387 location_entry_->SetForcedQuery(); |
| 369 } | 388 } |
| 370 | 389 |
| 371 void LocationBarViewGtk::UpdatePageActions() { | 390 void LocationBarViewGtk::UpdatePageActions() { |
| 372 // http://code.google.com/p/chromium/issues/detail?id=11973 | 391 std::vector<PageAction*> page_actions; |
| 392 if (profile_->GetExtensionsService()) |
| 393 page_actions = profile_->GetExtensionsService()->GetPageActions(); |
| 394 |
| 395 // Initialize on the first call, or re-inialize if more extensions have been |
| 396 // loaded or added after startup. |
| 397 if (page_actions.size() != page_action_views_.size()) { |
| 398 page_action_views_.reset(); // Delete the old views (if any). |
| 399 |
| 400 for (size_t i = 0; i < page_actions.size(); ++i) { |
| 401 page_action_views_.push_back( |
| 402 new PageActionViewGtk(this, profile_, page_actions[i])); |
| 403 gtk_box_pack_end(GTK_BOX(page_action_hbox_), |
| 404 page_action_views_[i]->widget(), FALSE, FALSE, 0); |
| 405 } |
| 406 } |
| 407 |
| 408 TabContents* contents = browser_->GetSelectedTabContents(); |
| 409 if (!page_action_views_.empty() && contents) { |
| 410 GURL url = GURL(WideToUTF8(toolbar_model_->GetText())); |
| 411 |
| 412 for (size_t i = 0; i < page_action_views_.size(); i++) |
| 413 page_action_views_[i]->UpdateVisibility(contents, url); |
| 414 } |
| 415 |
| 416 // If there are no visible page actions, hide the hbox too, so that it does |
| 417 // not affect the padding in the location bar. |
| 418 if (PageActionVisibleCount()) |
| 419 gtk_widget_show(page_action_hbox_); |
| 420 else |
| 421 gtk_widget_hide(page_action_hbox_); |
| 373 } | 422 } |
| 374 | 423 |
| 375 void LocationBarViewGtk::SaveStateToContents(TabContents* contents) { | 424 void LocationBarViewGtk::SaveStateToContents(TabContents* contents) { |
| 376 location_entry_->SaveStateToTab(contents); | 425 location_entry_->SaveStateToTab(contents); |
| 377 } | 426 } |
| 378 | 427 |
| 379 void LocationBarViewGtk::Revert() { | 428 void LocationBarViewGtk::Revert() { |
| 380 location_entry_->RevertAll(); | 429 location_entry_->RevertAll(); |
| 381 } | 430 } |
| 382 | 431 |
| 383 int LocationBarViewGtk::PageActionVisibleCount() { | 432 int LocationBarViewGtk::PageActionVisibleCount() { |
| 384 NOTIMPLEMENTED(); | 433 int count = 0; |
| 385 return -1; | 434 gtk_container_foreach(GTK_CONTAINER(page_action_hbox_), CountVisibleWidgets, |
| 435 &count); |
| 436 return count; |
| 386 } | 437 } |
| 387 | 438 |
| 388 void LocationBarViewGtk::Observe(NotificationType type, | 439 void LocationBarViewGtk::Observe(NotificationType type, |
| 389 const NotificationSource& source, | 440 const NotificationSource& source, |
| 390 const NotificationDetails& details) { | 441 const NotificationDetails& details) { |
| 391 DCHECK_EQ(type.value, NotificationType::BROWSER_THEME_CHANGED); | 442 DCHECK_EQ(type.value, NotificationType::BROWSER_THEME_CHANGED); |
| 392 | 443 |
| 393 if (theme_provider_->UseGtkTheme()) { | 444 if (theme_provider_->UseGtkTheme()) { |
| 394 gtk_widget_modify_bg(tab_to_search_box_, GTK_STATE_NORMAL, NULL); | 445 gtk_widget_modify_bg(tab_to_search_box_, GTK_STATE_NORMAL, NULL); |
| 395 | 446 |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 LocationBarViewGtk* location_bar) { | 644 LocationBarViewGtk* location_bar) { |
| 594 TabContents* tab = BrowserList::GetLastActive()->GetSelectedTabContents(); | 645 TabContents* tab = BrowserList::GetLastActive()->GetSelectedTabContents(); |
| 595 NavigationEntry* nav_entry = tab->controller().GetActiveEntry(); | 646 NavigationEntry* nav_entry = tab->controller().GetActiveEntry(); |
| 596 if (!nav_entry) { | 647 if (!nav_entry) { |
| 597 NOTREACHED(); | 648 NOTREACHED(); |
| 598 return true; | 649 return true; |
| 599 } | 650 } |
| 600 tab->ShowPageInfo(nav_entry->url(), nav_entry->ssl(), true); | 651 tab->ShowPageInfo(nav_entry->url(), nav_entry->ssl(), true); |
| 601 return true; | 652 return true; |
| 602 } | 653 } |
| 654 |
| 655 //////////////////////////////////////////////////////////////////////////////// |
| 656 // LocationBarViewGtk::PageActionViewGtk |
| 657 |
| 658 LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( |
| 659 LocationBarViewGtk* owner, Profile* profile, const PageAction* page_action) |
| 660 : owner_(owner), |
| 661 profile_(profile), |
| 662 page_action_(page_action) { |
| 663 event_box_.Own(gtk_event_box_new()); |
| 664 // Make the event box not visible so it does not paint a background. |
| 665 gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box_.get()), FALSE); |
| 666 g_signal_connect(event_box_.get(), "button-press-event", |
| 667 G_CALLBACK(&OnButtonPressed), this); |
| 668 |
| 669 image_.Own(gtk_image_new()); |
| 670 gtk_container_add(GTK_CONTAINER(event_box_.get()), image_.get()); |
| 671 |
| 672 Extension* extension = profile->GetExtensionsService()->GetExtensionById( |
| 673 page_action->extension_id()); |
| 674 DCHECK(extension); |
| 675 |
| 676 DCHECK(!page_action->icon_paths().empty()); |
| 677 const std::vector<std::string>& icon_paths = page_action->icon_paths(); |
| 678 pixbufs_.resize(icon_paths.size()); |
| 679 tracker_ = new ImageLoadingTracker(this, icon_paths.size()); |
| 680 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); |
| 681 iter != icon_paths.end(); ++iter) { |
| 682 tracker_->PostLoadImageTask(extension->GetResourcePath(*iter)); |
| 683 } |
| 684 } |
| 685 |
| 686 LocationBarViewGtk::PageActionViewGtk::~PageActionViewGtk() { |
| 687 if (tracker_) |
| 688 tracker_->StopTrackingImageLoad(); |
| 689 image_.Destroy(); |
| 690 event_box_.Destroy(); |
| 691 for (size_t i=0; i < pixbufs_.size(); ++i) { |
| 692 if (pixbufs_[i]) |
| 693 g_object_unref(pixbufs_[i]); |
| 694 } |
| 695 } |
| 696 |
| 697 void LocationBarViewGtk::PageActionViewGtk::UpdateVisibility( |
| 698 TabContents* contents, GURL url) { |
| 699 // Save this off so we can pass it back to the extension when the action gets |
| 700 // executed. See PageActionImageView::OnMousePressed. |
| 701 current_tab_id_ = ExtensionTabUtil::GetTabId(contents); |
| 702 current_url_ = url; |
| 703 |
| 704 const PageActionState* state = contents->GetPageActionState(page_action_); |
| 705 bool visible = state != NULL; |
| 706 if (visible) { |
| 707 // Set the tooltip. |
| 708 if (state->title().empty()) |
| 709 gtk_widget_set_tooltip_text(event_box_.get(), |
| 710 page_action_->name().c_str()); |
| 711 else |
| 712 gtk_widget_set_tooltip_text(event_box_.get(), state->title().c_str()); |
| 713 // Set the image. |
| 714 int index = state->icon_index(); |
| 715 // The image index (if not within bounds) will be set to the first image. |
| 716 if (index < 0 || index >= static_cast<int>(pixbufs_.size())) |
| 717 index = 0; |
| 718 // The pixbuf might not be loaded yet. |
| 719 if (pixbufs_[index]) |
| 720 gtk_image_set_from_pixbuf(GTK_IMAGE(image_.get()), pixbufs_[index]); |
| 721 else |
| 722 visible = false; |
| 723 } |
| 724 |
| 725 if (visible) { |
| 726 gtk_widget_show_all(event_box_.get()); |
| 727 } else { |
| 728 gtk_widget_hide_all(event_box_.get()); |
| 729 } |
| 730 } |
| 731 |
| 732 void LocationBarViewGtk::PageActionViewGtk::OnImageLoaded(SkBitmap* image, |
| 733 size_t index) { |
| 734 DCHECK(index < pixbufs_.size()); |
| 735 if (index == pixbufs_.size() - 1) |
| 736 tracker_ = NULL; // The tracker object will delete itself when we return. |
| 737 pixbufs_[index] = gfx::GdkPixbufFromSkBitmap(image); |
| 738 owner_->UpdatePageActions(); |
| 739 } |
| 740 |
| 741 // static |
| 742 gboolean LocationBarViewGtk::PageActionViewGtk::OnButtonPressed( |
| 743 GtkWidget* sender, |
| 744 GdkEventButton* event, |
| 745 LocationBarViewGtk::PageActionViewGtk* page_action_view) { |
| 746 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( |
| 747 page_action_view->profile_, |
| 748 page_action_view->page_action_->extension_id(), |
| 749 page_action_view->page_action_->id(), |
| 750 page_action_view->current_tab_id_, |
| 751 page_action_view->current_url_.spec()); |
| 752 return true; |
| 753 } |
| OLD | NEW |