OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/gtk/global_error_bubble.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
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" | |
22 | |
23 namespace { | |
24 | |
25 // Text size of the message label. 12.1px = 9pt @ 96dpi. | |
26 const double kMessageTextSize = 12.1; | |
27 | |
28 // Minimum width of the message label. | |
29 const int kMinMessageLabelWidth = 250; | |
30 | |
31 } // namespace | |
32 | |
33 GlobalErrorBubble::GlobalErrorBubble( | |
34 Browser* browser, | |
35 const base::WeakPtr<GlobalErrorWithStandardBubble>& error, | |
36 GtkWidget* anchor) | |
37 : browser_(browser), | |
38 bubble_(NULL), | |
39 error_(error), | |
40 message_width_(kMinMessageLabelWidth) { | |
41 DCHECK(browser_); | |
42 DCHECK(error_.get()); | |
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); | |
47 | |
48 GtkThemeService* theme_service = | |
49 GtkThemeService::GetFrom(browser_->profile()); | |
50 | |
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); | |
55 | |
56 GtkWidget* title_label = theme_service->BuildLabel( | |
57 base::UTF16ToUTF8(error_->GetBubbleViewTitle()), | |
58 ui::kGdkBlack); | |
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]), | |
63 ui::kGdkBlack)); | |
64 gtk_util::ForceFontSizePixels(message_labels_[i], kMessageTextSize); | |
65 } | |
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()) { | |
73 cancel_button = | |
74 gtk_button_new_with_label(base::UTF16ToUTF8(cancel_string).c_str()); | |
75 } | |
76 | |
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); | |
82 | |
83 // Middle, messages. | |
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); | |
87 | |
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); | |
93 if (cancel_button) | |
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); | |
97 | |
98 gtk_widget_grab_focus(accept_button); | |
99 | |
100 g_signal_connect(accept_button, "clicked", | |
101 G_CALLBACK(OnAcceptButtonThunk), this); | |
102 if (cancel_button) { | |
103 g_signal_connect(cancel_button, "clicked", | |
104 G_CALLBACK(OnCancelButtonThunk), this); | |
105 } | |
106 | |
107 g_signal_connect(top, "realize", G_CALLBACK(OnRealizeThunk), this); | |
108 g_signal_connect(bottom, "realize", G_CALLBACK(OnRealizeThunk), this); | |
109 | |
110 bubble_ = BubbleGtk::Show(anchor, | |
111 NULL, | |
112 content, | |
113 BubbleGtk::ANCHOR_TOP_RIGHT, | |
114 BubbleGtk::MATCH_SYSTEM_THEME | | |
115 BubbleGtk::POPUP_WINDOW | | |
116 BubbleGtk::GRAB_INPUT, | |
117 theme_service, | |
118 this); // error_ | |
119 } | |
120 | |
121 GlobalErrorBubble::~GlobalErrorBubble() { | |
122 } | |
123 | |
124 void GlobalErrorBubble::BubbleClosing(BubbleGtk* bubble, | |
125 bool closed_by_escape) { | |
126 if (error_.get()) | |
127 error_->BubbleViewDidClose(browser_); | |
128 } | |
129 | |
130 void GlobalErrorBubble::OnDestroy(GtkWidget* sender) { | |
131 delete this; | |
132 } | |
133 | |
134 void GlobalErrorBubble::OnAcceptButton(GtkWidget* sender) { | |
135 if (error_.get()) | |
136 error_->BubbleViewAcceptButtonPressed(browser_); | |
137 bubble_->Close(); | |
138 } | |
139 | |
140 void GlobalErrorBubble::OnCancelButton(GtkWidget* sender) { | |
141 if (error_.get()) | |
142 error_->BubbleViewCancelButtonPressed(browser_); | |
143 bubble_->Close(); | |
144 } | |
145 | |
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_); | |
151 } | |
152 | |
153 void GlobalErrorBubble::CloseBubbleView() { | |
154 bubble_->Close(); | |
155 } | |
156 | |
157 GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView( | |
158 Browser* browser, | |
159 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) { | |
160 BrowserWindowGtk* browser_window = | |
161 BrowserWindowGtk::GetBrowserWindowForNativeWindow( | |
162 browser->window()->GetNativeWindow()); | |
163 GtkWidget* anchor = browser_window->GetToolbar()->GetAppMenuButton(); | |
164 | |
165 // The bubble will be automatically deleted when it's closed. | |
166 return new GlobalErrorBubble(browser, error, anchor); | |
167 } | |
OLD | NEW |