OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/gtk/web_intent_picker_gtk.h" |
| 6 |
| 7 #include "chrome/browser/favicon/favicon_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/gtk/custom_button.h" |
| 10 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
| 11 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 12 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" |
| 13 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 15 #include "content/browser/tab_contents/tab_contents.h" |
| 16 #include "content/common/content_notification_types.h" |
| 17 #include "googleurl/src/gurl.h" |
| 18 #include "grit/generated_resources.h" |
| 19 #include "grit/ui_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/gfx/gtk_util.h" |
| 23 #include "ui/gfx/image/image.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 GtkThemeService *GetThemeService(TabContents* tab_contents) { |
| 28 Profile* profile = Profile::FromBrowserContext( |
| 29 tab_contents->browser_context()); |
| 30 return GtkThemeService::GetFrom(profile); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 // static |
| 36 WebIntentPicker* WebIntentPicker::Create(TabContents* tab_contents, |
| 37 WebIntentPickerDelegate* delegate) { |
| 38 return new WebIntentPickerGtk(tab_contents, delegate); |
| 39 } |
| 40 |
| 41 WebIntentPickerGtk::WebIntentPickerGtk(TabContents* tab_contents, |
| 42 WebIntentPickerDelegate* delegate) |
| 43 : tab_contents_(tab_contents), |
| 44 delegate_(delegate), |
| 45 window_(NULL) { |
| 46 DCHECK(delegate_ != NULL); |
| 47 root_.Own(gtk_vbox_new(FALSE, gtk_util::kContentAreaBorder)); |
| 48 GtkWidget* hbox = gtk_hbox_new(FALSE, 0); |
| 49 GtkWidget* label = gtk_label_new( |
| 50 l10n_util::GetStringUTF8(IDS_CHOOSE_INTENT_HANDLER_MESSAGE).c_str()); |
| 51 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); |
| 52 |
| 53 close_button_.reset( |
| 54 CustomDrawButton::CloseButton(GetThemeService(tab_contents_))); |
| 55 g_signal_connect(close_button_->widget(), |
| 56 "clicked", |
| 57 G_CALLBACK(OnCloseButtonClickThunk), |
| 58 this); |
| 59 gtk_widget_set_can_focus(close_button_->widget(), FALSE); |
| 60 gtk_box_pack_end(GTK_BOX(hbox), close_button_->widget(), FALSE, FALSE, 0); |
| 61 |
| 62 gtk_box_pack_start(GTK_BOX(root_.get()), hbox, FALSE, FALSE, 0); |
| 63 |
| 64 button_hbox_ = gtk_hbox_new(FALSE, gtk_util::kContentAreaBorder); |
| 65 gtk_box_pack_start(GTK_BOX(root_.get()), button_hbox_, TRUE, TRUE, 0); |
| 66 } |
| 67 |
| 68 WebIntentPickerGtk::~WebIntentPickerGtk() { |
| 69 DCHECK(window_ == NULL) << "Should have closed window before destroying."; |
| 70 } |
| 71 |
| 72 void WebIntentPickerGtk::SetServiceURLs(const std::vector<GURL>& urls) { |
| 73 for (size_t i = 0; i < urls.size(); ++i) { |
| 74 GtkWidget* button = gtk_button_new(); |
| 75 gtk_widget_set_tooltip_text(button, urls[i].spec().c_str()); |
| 76 gtk_box_pack_start(GTK_BOX(button_hbox_), button, FALSE, FALSE, 0); |
| 77 g_signal_connect(button, |
| 78 "clicked", |
| 79 G_CALLBACK(OnServiceButtonClickThunk), |
| 80 this); |
| 81 } |
| 82 |
| 83 gtk_widget_show_all(button_hbox_); |
| 84 } |
| 85 |
| 86 void WebIntentPickerGtk::SetServiceIcon(size_t index, const SkBitmap& icon) { |
| 87 if (icon.empty()) |
| 88 return; |
| 89 |
| 90 GList* button_list = gtk_container_get_children(GTK_CONTAINER(button_hbox_)); |
| 91 GtkButton* button = GTK_BUTTON(g_list_nth_data(button_list, index)); |
| 92 DCHECK(button != NULL) << "Invalid index."; |
| 93 |
| 94 GdkPixbuf* icon_pixbuf = gfx::GdkPixbufFromSkBitmap(&icon); |
| 95 gtk_button_set_image(button, gtk_image_new_from_pixbuf(icon_pixbuf)); |
| 96 g_object_unref(icon_pixbuf); |
| 97 } |
| 98 |
| 99 void WebIntentPickerGtk::SetDefaultServiceIcon(size_t index) { |
| 100 GList* button_list = gtk_container_get_children(GTK_CONTAINER(button_hbox_)); |
| 101 GtkButton* button = GTK_BUTTON(g_list_nth_data(button_list, index)); |
| 102 DCHECK(button != NULL) << "Invalid index."; |
| 103 |
| 104 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 105 GdkPixbuf* icon_pixbuf = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON); |
| 106 gtk_button_set_image(button, gtk_image_new_from_pixbuf(icon_pixbuf)); |
| 107 } |
| 108 |
| 109 void WebIntentPickerGtk::Show() { |
| 110 DCHECK(window_ == NULL) << "Show already called."; |
| 111 window_ = tab_contents_->CreateConstrainedDialog(this); |
| 112 } |
| 113 |
| 114 void WebIntentPickerGtk::Close() { |
| 115 DCHECK(window_ != NULL) << "Show not called."; |
| 116 window_->CloseConstrainedWindow(); |
| 117 window_ = NULL; |
| 118 } |
| 119 |
| 120 GtkWidget* WebIntentPickerGtk::GetWidgetRoot() { |
| 121 return root_.get(); |
| 122 } |
| 123 |
| 124 GtkWidget* WebIntentPickerGtk::GetFocusWidget() { |
| 125 return root_.get(); |
| 126 } |
| 127 |
| 128 void WebIntentPickerGtk::DeleteDelegate() { |
| 129 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 130 } |
| 131 |
| 132 void WebIntentPickerGtk::OnCloseButtonClick(GtkWidget* button) { |
| 133 delegate_->OnCancelled(); |
| 134 } |
| 135 |
| 136 void WebIntentPickerGtk::OnServiceButtonClick(GtkWidget* button) { |
| 137 GList* button_list = gtk_container_get_children(GTK_CONTAINER(button_hbox_)); |
| 138 gint index = g_list_index(button_list, button); |
| 139 DCHECK(index != -1); |
| 140 |
| 141 delegate_->OnServiceChosen(static_cast<size_t>(index)); |
| 142 } |
OLD | NEW |