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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
// 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/infobars/infobar_gtk.h"
#include "base/debug/trace_event.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/infobars/infobar_tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/gtk/browser_window_gtk.h"
#include "chrome/browser/ui/gtk/custom_button.h"
#include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
#include "chrome/browser/ui/gtk/gtk_theme_service.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/browser/ui/gtk/infobars/infobar_container_gtk.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/gtk/gtk_expanded_container.h"
#include "ui/base/gtk/gtk_hig_constants.h"
#include "ui/base/gtk/gtk_signal_registrar.h"
#include "ui/base/models/menu_model.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/image/image.h"
namespace {
// Pixels between infobar elements.
const int kElementPadding = 5;
// Extra padding on either end of info bar.
const int kLeftPadding = 5;
const int kRightPadding = 5;
// Spacing between buttons.
const int kButtonButtonSpacing = 3;
} // namespace
// static
const int InfoBar::kSeparatorLineHeight = 1;
const int InfoBar::kDefaultArrowTargetHeight = 9;
const int InfoBar::kMaximumArrowTargetHeight = 24;
const int InfoBar::kDefaultArrowTargetHalfWidth = kDefaultArrowTargetHeight;
const int InfoBar::kMaximumArrowTargetHalfWidth = 14;
const int InfoBar::kDefaultBarTargetHeight = 36;
// static
const int InfoBarGtk::kEndOfLabelSpacing = 6;
InfoBarGtk::InfoBarGtk(InfoBarTabHelper* owner, InfoBarDelegate* delegate)
: InfoBar(owner, delegate),
theme_service_(GtkThemeService::GetFrom(Profile::FromBrowserContext(
owner->web_contents()->GetBrowserContext()))),
signals_(new ui::GtkSignalRegistrar) {
DCHECK(delegate);
// Create |hbox_| and pad the sides.
hbox_ = gtk_hbox_new(FALSE, kElementPadding);
// Make the whole infor bar horizontally shrinkable.
gtk_widget_set_size_request(hbox_, 0, -1);
GtkWidget* padding = gtk_alignment_new(0, 0, 1, 1);
gtk_alignment_set_padding(GTK_ALIGNMENT(padding),
0, 0, kLeftPadding, kRightPadding);
bg_box_ = gtk_event_box_new();
gtk_widget_set_app_paintable(bg_box_, TRUE);
g_signal_connect(bg_box_, "expose-event",
G_CALLBACK(OnBackgroundExposeThunk), this);
gtk_container_add(GTK_CONTAINER(padding), hbox_);
gtk_container_add(GTK_CONTAINER(bg_box_), padding);
// Add the icon on the left, if any.
gfx::Image* icon = delegate->GetIcon();
if (icon) {
GtkWidget* image = gtk_image_new_from_pixbuf(*icon);
gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.5);
gtk_box_pack_start(GTK_BOX(hbox_), image, FALSE, FALSE, 0);
}
close_button_.reset(CustomDrawButton::CloseButton(theme_service_));
gtk_util::CenterWidgetInHBox(hbox_, close_button_->widget(), true, 0);
signals_->Connect(close_button_->widget(), "clicked",
G_CALLBACK(OnCloseButtonThunk), this);
widget_.Own(gtk_expanded_container_new());
gtk_container_add(GTK_CONTAINER(widget_.get()), bg_box_);
gtk_widget_set_size_request(widget_.get(), -1, 0);
g_signal_connect(widget_.get(), "child-size-request",
G_CALLBACK(OnChildSizeRequestThunk),
this);
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
content::Source<ThemeService>(theme_service_));
UpdateBorderColor();
}
InfoBarGtk::~InfoBarGtk() {
}
GtkWidget* InfoBarGtk::widget() {
return widget_.get();
}
GdkColor InfoBarGtk::GetBorderColor() const {
return theme_service_->GetBorderColor();
}
int InfoBarGtk::AnimatingHeight() const {
return animation().is_animating() ? bar_target_height() : 0;
}
ui::GtkSignalRegistrar* InfoBarGtk::Signals() {
return signals_.get();
}
GtkWidget* InfoBarGtk::CreateLabel(const std::string& text) {
return theme_service_->BuildLabel(text, ui::kGdkBlack);
}
GtkWidget* InfoBarGtk::CreateLinkButton(const std::string& text) {
return theme_service_->BuildChromeLinkButton(text);
}
SkColor InfoBarGtk::ConvertGetColor(ColorGetter getter) {
double r, g, b;
(this->*getter)(delegate()->GetInfoBarType(), &r, &g, &b);
return SkColorSetARGB(255, 255 * r, 255 * g, 255 * b);
}
void InfoBarGtk::AddLabelWithInlineLink(const string16& display_text,
const string16& link_text,
size_t link_offset,
GCallback callback) {
GtkWidget* link_button = CreateLinkButton(UTF16ToUTF8(link_text));
gtk_util::ForceFontSizePixels(
GTK_CHROME_LINK_BUTTON(link_button)->label, 13.4);
DCHECK(callback);
signals_->Connect(link_button, "clicked", callback, this);
gtk_util::SetButtonTriggersNavigation(link_button);
GtkWidget* hbox = gtk_hbox_new(FALSE, 0);
// We want the link to be horizontally shrinkable, so that the Chrome
// window can be resized freely even with a very long link.
gtk_widget_set_size_request(hbox, 0, -1);
gtk_box_pack_start(GTK_BOX(hbox_), hbox, TRUE, TRUE, 0);
// Need to insert the link inside the display text.
GtkWidget* initial_label = CreateLabel(
UTF16ToUTF8(display_text.substr(0, link_offset)));
GtkWidget* trailing_label = CreateLabel(
UTF16ToUTF8(display_text.substr(link_offset)));
gtk_util::ForceFontSizePixels(initial_label, 13.4);
gtk_util::ForceFontSizePixels(trailing_label, 13.4);
// TODO(joth): None of the label widgets are set as shrinkable here, meaning
// the text will run under the close button etc. when the width is restricted,
// rather than eliding.
// We don't want any spacing between the elements, so we pack them into
// this hbox that doesn't use kElementPadding.
gtk_box_pack_start(GTK_BOX(hbox), initial_label, FALSE, FALSE, 0);
gtk_util::CenterWidgetInHBox(hbox, link_button, false, 0);
gtk_box_pack_start(GTK_BOX(hbox), trailing_label, FALSE, FALSE, 0);
}
void InfoBarGtk::ShowMenuWithModel(GtkWidget* sender,
MenuGtk::Delegate* delegate,
ui::MenuModel* model) {
menu_model_.reset(model);
menu_.reset(new MenuGtk(delegate, menu_model_.get()));
menu_->PopupForWidget(sender, 1, gtk_get_current_event_time());
}
void InfoBarGtk::GetTopColor(InfoBarDelegate::Type type,
double* r, double* g, double* b) {
SkColor color = theme_service_->UsingNativeTheme() ?
theme_service_->GetColor(ThemeService::COLOR_TOOLBAR) :
GetInfoBarTopColor(type);
*r = SkColorGetR(color) / 255.0;
*g = SkColorGetG(color) / 255.0;
*b = SkColorGetB(color) / 255.0;
}
void InfoBarGtk::GetBottomColor(InfoBarDelegate::Type type,
double* r, double* g, double* b) {
SkColor color = theme_service_->UsingNativeTheme() ?
theme_service_->GetColor(ThemeService::COLOR_TOOLBAR) :
GetInfoBarBottomColor(type);
*r = SkColorGetR(color) / 255.0;
*g = SkColorGetG(color) / 255.0;
*b = SkColorGetB(color) / 255.0;
}
void InfoBarGtk::UpdateBorderColor() {
gtk_widget_queue_draw(widget());
}
void InfoBarGtk::OnCloseButton(GtkWidget* button) {
// If we're not owned, we're already closing, so don't call
// InfoBarDismissed(), since this can lead to us double-recording dismissals.
if (delegate() && owned())
delegate()->InfoBarDismissed();
RemoveSelf();
}
gboolean InfoBarGtk::OnBackgroundExpose(GtkWidget* sender,
GdkEventExpose* event) {
TRACE_EVENT0("ui::gtk", "InfoBarGtk::OnBackgroundExpose");
GtkAllocation allocation;
gtk_widget_get_allocation(sender, &allocation);
const int height = allocation.height;
cairo_t* cr = gdk_cairo_create(GDK_DRAWABLE(sender->window));
gdk_cairo_rectangle(cr, &event->area);
cairo_clip(cr);
cairo_pattern_t* pattern = cairo_pattern_create_linear(0, 0, 0, height);
double top_r, top_g, top_b;
GetTopColor(delegate()->GetInfoBarType(), &top_r, &top_g, &top_b);
cairo_pattern_add_color_stop_rgb(pattern, 0.0, top_r, top_g, top_b);
double bottom_r, bottom_g, bottom_b;
GetBottomColor(delegate()->GetInfoBarType(), &bottom_r, &bottom_g, &bottom_b);
cairo_pattern_add_color_stop_rgb(
pattern, 1.0, bottom_r, bottom_g, bottom_b);
cairo_set_source(cr, pattern);
cairo_paint(cr);
cairo_pattern_destroy(pattern);
// Draw the bottom border.
GdkColor border_color = GetBorderColor();
cairo_set_source_rgb(cr, border_color.red / 65535.0,
border_color.green / 65535.0,
border_color.blue / 65535.0);
cairo_set_line_width(cr, 1.0);
cairo_move_to(cr, 0, allocation.height - 0.5);
cairo_rel_line_to(cr, allocation.width, 0);
cairo_stroke(cr);
cairo_destroy(cr);
if (container()) {
static_cast<InfoBarContainerGtk*>(container())->
PaintInfobarBitsOn(sender, event, this);
}
return FALSE;
}
void InfoBarGtk::PlatformSpecificShow(bool animate) {
gtk_widget_show_all(widget_.get());
gtk_widget_set_size_request(widget_.get(), -1, bar_height());
if (bg_box_->window)
gdk_window_lower(bg_box_->window);
}
void InfoBarGtk::PlatformSpecificOnCloseSoon() {
// We must close all menus and prevent any signals from being emitted while
// we are animating the info bar closed.
menu_.reset();
menu_model_.reset();
signals_.reset();
}
void InfoBarGtk::PlatformSpecificOnHeightsRecalculated() {
gtk_widget_set_size_request(bg_box_, -1, bar_target_height());
gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(widget_.get()),
bg_box_, 0,
bar_height() - bar_target_height());
gtk_widget_set_size_request(widget_.get(), -1, bar_height());
gtk_widget_queue_draw(widget_.get());
}
void InfoBarGtk::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
UpdateBorderColor();
}
void InfoBarGtk::OnChildSizeRequest(GtkWidget* expanded,
GtkWidget* child,
GtkRequisition* requisition) {
requisition->height = -1;
}
|