summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/web_intent_picker_gtk.cc
blob: 90c790187550ff355a88388998aa1ac37696bbd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/gtk/web_intent_picker_gtk.h"

#include "chrome/browser/favicon/favicon_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/gtk/custom_button.h"
#include "chrome/browser/ui/gtk/gtk_theme_service.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/browser/ui/intents/web_intent_picker_controller.h"
#include "chrome/browser/ui/intents/web_intent_picker_delegate.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/common/content_notification_types.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
#include "grit/ui_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/image/image.h"

namespace {

GtkThemeService *GetThemeService(TabContents* tab_contents) {
  Profile* profile = Profile::FromBrowserContext(
      tab_contents->browser_context());
  return GtkThemeService::GetFrom(profile);
}

} // namespace

// static
WebIntentPicker* WebIntentPicker::Create(TabContents* tab_contents,
                                         WebIntentPickerDelegate* delegate) {
  return new WebIntentPickerGtk(tab_contents, delegate);
}

WebIntentPickerGtk::WebIntentPickerGtk(TabContents* tab_contents,
                                       WebIntentPickerDelegate* delegate)
    : tab_contents_(tab_contents),
      delegate_(delegate),
      window_(NULL) {
  DCHECK(delegate_ != NULL);
  root_.Own(gtk_vbox_new(FALSE, gtk_util::kContentAreaBorder));
  GtkWidget* hbox = gtk_hbox_new(FALSE, 0);
  GtkWidget* label = gtk_label_new(
      l10n_util::GetStringUTF8(IDS_CHOOSE_INTENT_HANDLER_MESSAGE).c_str());
  gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);

  close_button_.reset(
      CustomDrawButton::CloseButton(GetThemeService(tab_contents_)));
  g_signal_connect(close_button_->widget(),
                   "clicked",
                   G_CALLBACK(OnCloseButtonClickThunk),
                   this);
  gtk_widget_set_can_focus(close_button_->widget(), FALSE);
  gtk_box_pack_end(GTK_BOX(hbox), close_button_->widget(), FALSE, FALSE, 0);

  gtk_box_pack_start(GTK_BOX(root_.get()), hbox, FALSE, FALSE, 0);

  button_hbox_ = gtk_hbox_new(FALSE, gtk_util::kContentAreaBorder);
  gtk_box_pack_start(GTK_BOX(root_.get()), button_hbox_, TRUE, TRUE, 0);
}

WebIntentPickerGtk::~WebIntentPickerGtk() {
  DCHECK(window_ == NULL) << "Should have closed window before destroying.";
}

void WebIntentPickerGtk::SetServiceURLs(const std::vector<GURL>& urls) {
  for (size_t i = 0; i < urls.size(); ++i) {
    GtkWidget* button = gtk_button_new();
    gtk_widget_set_tooltip_text(button, urls[i].spec().c_str());
    gtk_box_pack_start(GTK_BOX(button_hbox_), button, FALSE, FALSE, 0);
    g_signal_connect(button,
                     "clicked",
                     G_CALLBACK(OnServiceButtonClickThunk),
                     this);
  }

  gtk_widget_show_all(button_hbox_);
}

void WebIntentPickerGtk::SetServiceIcon(size_t index, const SkBitmap& icon) {
  if (icon.empty())
    return;

  GList* button_list = gtk_container_get_children(GTK_CONTAINER(button_hbox_));
  GtkButton* button = GTK_BUTTON(g_list_nth_data(button_list, index));
  DCHECK(button != NULL) << "Invalid index.";

  GdkPixbuf* icon_pixbuf = gfx::GdkPixbufFromSkBitmap(&icon);
  gtk_button_set_image(button, gtk_image_new_from_pixbuf(icon_pixbuf));
  g_object_unref(icon_pixbuf);
}

void WebIntentPickerGtk::SetDefaultServiceIcon(size_t index) {
  GList* button_list = gtk_container_get_children(GTK_CONTAINER(button_hbox_));
  GtkButton* button = GTK_BUTTON(g_list_nth_data(button_list, index));
  DCHECK(button != NULL) << "Invalid index.";

  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  GdkPixbuf* icon_pixbuf = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON);
  gtk_button_set_image(button, gtk_image_new_from_pixbuf(icon_pixbuf));
}

void WebIntentPickerGtk::Show() {
  DCHECK(window_ == NULL) << "Show already called.";
  window_ = tab_contents_->CreateConstrainedDialog(this);
}

void WebIntentPickerGtk::Close() {
  DCHECK(window_ != NULL) << "Show not called.";
  window_->CloseConstrainedWindow();
  window_ = NULL;
}

GtkWidget* WebIntentPickerGtk::GetWidgetRoot() {
  return root_.get();
}

GtkWidget* WebIntentPickerGtk::GetFocusWidget() {
  return root_.get();
}

void WebIntentPickerGtk::DeleteDelegate() {
  MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

void WebIntentPickerGtk::OnCloseButtonClick(GtkWidget* button) {
  delegate_->OnCancelled();
}

void WebIntentPickerGtk::OnServiceButtonClick(GtkWidget* button) {
  GList* button_list = gtk_container_get_children(GTK_CONTAINER(button_hbox_));
  gint index = g_list_index(button_list, button);
  DCHECK(index != -1);

  delegate_->OnServiceChosen(static_cast<size_t>(index));
}