1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/gtk/global_error_bubble.h"
9 #include "base/i18n/rtl.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/global_error/global_error.h"
13 #include "chrome/browser/ui/global_error/global_error_service.h"
14 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
15 #include "chrome/browser/ui/gtk/browser_toolbar_gtk.h"
16 #include "chrome/browser/ui/gtk/browser_window_gtk.h"
17 #include "chrome/browser/ui/gtk/gtk_theme_service.h"
18 #include "chrome/browser/ui/gtk/gtk_util.h"
19 #include "ui/base/gtk/gtk_hig_constants.h"
20 #include "ui/gfx/gtk_util.h"
21 #include "ui/gfx/image/image.h"
25 // Text size of the message label. 12.1px = 9pt @ 96dpi.
26 const double kMessageTextSize = 12.1;
28 // Minimum width of the message label.
29 const int kMinMessageLabelWidth = 250;
33 GlobalErrorBubble::GlobalErrorBubble(
35 const base::WeakPtr<GlobalErrorWithStandardBubble>& error,
40 message_width_(kMinMessageLabelWidth) {
43 GtkWidget* content = gtk_vbox_new(FALSE, ui::kControlSpacing);
44 gtk_container_set_border_width(GTK_CONTAINER(content),
45 ui::kContentAreaBorder);
46 g_signal_connect(content, "destroy", G_CALLBACK(OnDestroyThunk), this);
48 GtkThemeService* theme_service =
49 GtkThemeService::GetFrom(browser_->profile());
51 gfx::Image image = error_->GetBubbleViewIcon();
52 CHECK(!image.IsEmpty());
53 GdkPixbuf* pixbuf = image.ToGdkPixbuf();
54 GtkWidget* image_view = gtk_image_new_from_pixbuf(pixbuf);
56 GtkWidget* title_label = theme_service->BuildLabel(
57 base::UTF16ToUTF8(error_->GetBubbleViewTitle()),
59 std::vector<base::string16> messages = error_->GetBubbleViewMessages();
60 for (size_t i = 0; i < messages.size(); ++i) {
61 message_labels_.push_back(theme_service->BuildLabel(
62 base::UTF16ToUTF8(messages[i]),
64 gtk_util::ForceFontSizePixels(message_labels_[i], kMessageTextSize);
66 // Message label will be sized later in "realize" callback because we need
67 // to know the width of the title and the width of the buttons group.
68 GtkWidget* accept_button = gtk_button_new_with_label(
69 base::UTF16ToUTF8(error_->GetBubbleViewAcceptButtonLabel()).c_str());
70 base::string16 cancel_string = error_->GetBubbleViewCancelButtonLabel();
71 GtkWidget* cancel_button = NULL;
72 if (!cancel_string.empty()) {
74 gtk_button_new_with_label(base::UTF16ToUTF8(cancel_string).c_str());
77 // Top, icon and title.
78 GtkWidget* top = gtk_hbox_new(FALSE, ui::kControlSpacing);
79 gtk_box_pack_start(GTK_BOX(top), image_view, FALSE, FALSE, 0);
80 gtk_box_pack_start(GTK_BOX(top), title_label, FALSE, FALSE, 0);
81 gtk_box_pack_start(GTK_BOX(content), top, FALSE, FALSE, 0);
84 for (size_t i = 0; i < message_labels_.size(); ++i)
85 gtk_box_pack_start(GTK_BOX(content), message_labels_[i], FALSE, FALSE, 0);
86 gtk_box_pack_start(GTK_BOX(content), gtk_hbox_new(FALSE, 0), FALSE, FALSE, 0);
88 // Bottom, accept and cancel button.
89 // We want the buttons on the right, so just use an expanding label to fill
90 // all of the extra space on the left.
91 GtkWidget* bottom = gtk_hbox_new(FALSE, ui::kControlSpacing);
92 gtk_box_pack_start(GTK_BOX(bottom), gtk_label_new(NULL), TRUE, TRUE, 0);
94 gtk_box_pack_start(GTK_BOX(bottom), cancel_button, FALSE, FALSE, 0);
95 gtk_box_pack_start(GTK_BOX(bottom), accept_button, FALSE, FALSE, 0);
96 gtk_box_pack_start(GTK_BOX(content), bottom, FALSE, FALSE, 0);
98 gtk_widget_grab_focus(accept_button);
100 g_signal_connect(accept_button, "clicked",
101 G_CALLBACK(OnAcceptButtonThunk), this);
103 g_signal_connect(cancel_button, "clicked",
104 G_CALLBACK(OnCancelButtonThunk), this);
107 g_signal_connect(top, "realize", G_CALLBACK(OnRealizeThunk), this);
108 g_signal_connect(bottom, "realize", G_CALLBACK(OnRealizeThunk), this);
110 bubble_ = BubbleGtk::Show(anchor,
113 BubbleGtk::ANCHOR_TOP_RIGHT,
114 BubbleGtk::MATCH_SYSTEM_THEME |
115 BubbleGtk::POPUP_WINDOW |
116 BubbleGtk::GRAB_INPUT,
121 GlobalErrorBubble::~GlobalErrorBubble() {
124 void GlobalErrorBubble::BubbleClosing(BubbleGtk* bubble,
125 bool closed_by_escape) {
127 error_->BubbleViewDidClose(browser_);
130 void GlobalErrorBubble::OnDestroy(GtkWidget* sender) {
134 void GlobalErrorBubble::OnAcceptButton(GtkWidget* sender) {
136 error_->BubbleViewAcceptButtonPressed(browser_);
140 void GlobalErrorBubble::OnCancelButton(GtkWidget* sender) {
142 error_->BubbleViewCancelButtonPressed(browser_);
146 void GlobalErrorBubble::OnRealize(GtkWidget* sender) {
147 int width = gtk_util::GetWidgetSize(sender).width();
148 message_width_ = std::max(message_width_, width);
149 for (size_t i = 0; i < message_labels_.size(); ++i)
150 gtk_util::SetLabelWidth(message_labels_[i], message_width_);
153 void GlobalErrorBubble::CloseBubbleView() {
157 GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView(
159 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) {
160 BrowserWindowGtk* browser_window =
161 BrowserWindowGtk::GetBrowserWindowForNativeWindow(
162 browser->window()->GetNativeWindow());
163 GtkWidget* anchor = browser_window->GetToolbar()->GetAppMenuButton();
165 // The bubble will be automatically deleted when it's closed.
166 return new GlobalErrorBubble(browser, error, anchor);