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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
// Copyright (c) 2009 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/gtk/infobar_gtk.h"
#include <gtk/gtk.h>
#include "base/utf_string_conversions.h"
#include "chrome/browser/gtk/custom_button.h"
#include "chrome/browser/gtk/gtk_chrome_link_button.h"
#include "chrome/browser/gtk/gtk_theme_provider.h"
#include "chrome/browser/gtk/gtk_util.h"
#include "chrome/browser/gtk/infobar_container_gtk.h"
#include "chrome/common/notification_service.h"
#include "gfx/gtk_util.h"
namespace {
// The total height of the info bar.
const int kInfoBarHeight = 37;
// Pixels between infobar elements.
const int kElementPadding = 5;
// Extra padding on either end of info bar.
const int kLeftPadding = 5;
const int kRightPadding = 5;
} // namespace
InfoBar::InfoBar(InfoBarDelegate* delegate)
: container_(NULL),
delegate_(delegate),
theme_provider_(NULL) {
// 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);
GtkWidget* 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);
// The -1 on the kInfoBarHeight is to account for the border.
gtk_widget_set_size_request(bg_box, -1, kInfoBarHeight - 1);
border_bin_.Own(gtk_util::CreateGtkBorderBin(bg_box, NULL,
0, 1, 0, 0));
// Add the icon on the left, if any.
SkBitmap* icon = delegate->GetIcon();
if (icon) {
GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(icon);
GtkWidget* image = gtk_image_new_from_pixbuf(pixbuf);
g_object_unref(pixbuf);
gtk_box_pack_start(GTK_BOX(hbox_), image, FALSE, FALSE, 0);
}
// TODO(erg): GTK theme the info bar.
close_button_.reset(CustomDrawButton::CloseButton(NULL));
gtk_util::CenterWidgetInHBox(hbox_, close_button_->widget(), true, 0);
g_signal_connect(close_button_->widget(), "clicked",
G_CALLBACK(OnCloseButtonThunk), this);
slide_widget_.reset(new SlideAnimatorGtk(border_bin_.get(),
SlideAnimatorGtk::DOWN,
0, true, true, this));
// We store a pointer back to |this| so we can refer to it from the infobar
// container.
g_object_set_data(G_OBJECT(slide_widget_->widget()), "info-bar", this);
}
InfoBar::~InfoBar() {
border_bin_.Destroy();
}
GtkWidget* InfoBar::widget() {
return slide_widget_->widget();
}
void InfoBar::AnimateOpen() {
slide_widget_->Open();
if (border_bin_->window)
gdk_window_lower(border_bin_->window);
}
void InfoBar::Open() {
slide_widget_->OpenWithoutAnimation();
if (border_bin_->window)
gdk_window_lower(border_bin_->window);
}
void InfoBar::AnimateClose() {
slide_widget_->Close();
}
void InfoBar::Close() {
if (delegate_) {
delegate_->InfoBarClosed();
delegate_ = NULL;
}
delete this;
}
bool InfoBar::IsAnimating() {
return slide_widget_->IsAnimating();
}
void InfoBar::RemoveInfoBar() const {
container_->RemoveDelegate(delegate_);
}
void InfoBar::Closed() {
Close();
}
void InfoBar::SetThemeProvider(GtkThemeProvider* theme_provider) {
if (theme_provider_) {
NOTREACHED();
return;
}
theme_provider_ = theme_provider;
registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
NotificationService::AllSources());
UpdateBorderColor();
}
void InfoBar::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
UpdateBorderColor();
}
// TODO(joth): This method factors out some common functionality between the
// various derived infobar classes, however the class hierarchy itself could
// use refactoring to reduce this duplication. http://crbug.com/38924
void InfoBar::AddLabelAndLink(const string16& display_text,
const string16& link_text,
size_t link_offset,
guint link_padding,
GCallback callback) {
GtkWidget* link_button = NULL;
if (link_text.empty()) {
// No link text, so skip creating the link and splitting display_text.
link_offset = string16::npos;
} else {
// If we have some link text, create the link button.
link_button = gtk_chrome_link_button_new(UTF16ToUTF8(link_text).c_str());
gtk_chrome_link_button_set_use_gtk_theme(
GTK_CHROME_LINK_BUTTON(link_button), FALSE);
DCHECK(callback);
g_signal_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);
// If link_offset is npos, we right-align the link instead of embedding it
// in the text.
if (link_offset == string16::npos) {
if (link_button)
gtk_box_pack_end(GTK_BOX(hbox), link_button, FALSE, FALSE, 0);
GtkWidget* label = gtk_label_new(UTF16ToUTF8(display_text).c_str());
// In order to avoid the link_button and the label overlapping with each
// other, we make the label shrinkable.
gtk_widget_set_size_request(label, 0, -1);
gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &gfx::kGdkBlack);
gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
} else {
DCHECK(link_button);
// Need to insert the link inside the display text.
GtkWidget* initial_label = gtk_label_new(
UTF16ToUTF8(display_text.substr(0, link_offset)).c_str());
GtkWidget* trailing_label = gtk_label_new(
UTF16ToUTF8(display_text.substr(link_offset)).c_str());
// TODO(joth): Unlike the right-align case above, 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.
gtk_widget_modify_fg(initial_label, GTK_STATE_NORMAL, &gfx::kGdkBlack);
gtk_widget_modify_fg(trailing_label, GTK_STATE_NORMAL, &gfx::kGdkBlack);
// 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, link_padding);
gtk_box_pack_start(GTK_BOX(hbox), trailing_label, FALSE, FALSE, 0);
}
}
void InfoBar::GetTopColor(InfoBarDelegate::Type type,
double* r, double* g, double *b) {
// These constants are copied from corresponding skia constants from
// browser/views/infobars/infobars.cc, and then changed into 0-1 ranged
// values for cairo.
switch (type) {
case InfoBarDelegate::WARNING_TYPE:
*r = 255.0 / 255.0;
*g = 242.0 / 255.0;
*b = 183.0 / 255.0;
break;
case InfoBarDelegate::PAGE_ACTION_TYPE:
*r = 218.0 / 255.0;
*g = 231.0 / 255.0;
*b = 249.0 / 255.0;
break;
}
}
void InfoBar::GetBottomColor(InfoBarDelegate::Type type,
double* r, double* g, double *b) {
switch (type) {
case InfoBarDelegate::WARNING_TYPE:
*r = 250.0 / 255.0;
*g = 230.0 / 255.0;
*b = 145.0 / 255.0;
break;
case InfoBarDelegate::PAGE_ACTION_TYPE:
*r = 179.0 / 255.0;
*g = 202.0 / 255.0;
*b = 231.0 / 255.0;
break;
}
}
void InfoBar::UpdateBorderColor() {
GdkColor border_color = theme_provider_->GetBorderColor();
gtk_widget_modify_bg(border_bin_.get(), GTK_STATE_NORMAL, &border_color);
}
void InfoBar::OnCloseButton(GtkWidget* button) {
if (delegate_)
delegate_->InfoBarDismissed();
RemoveInfoBar();
}
gboolean InfoBar::OnBackgroundExpose(GtkWidget* widget,
GdkEventExpose* event) {
const int height = widget->allocation.height;
cairo_t* cr = gdk_cairo_create(GDK_DRAWABLE(widget->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);
cairo_destroy(cr);
return FALSE;
}
// AlertInfoBar ----------------------------------------------------------------
class AlertInfoBar : public InfoBar {
public:
explicit AlertInfoBar(AlertInfoBarDelegate* delegate)
: InfoBar(delegate) {
AddLabelAndLink(delegate->GetMessageText(), string16(), 0, 0, NULL);
gtk_widget_show_all(border_bin_.get());
}
};
// LinkInfoBar -----------------------------------------------------------------
class LinkInfoBar : public InfoBar {
public:
explicit LinkInfoBar(LinkInfoBarDelegate* delegate)
: InfoBar(delegate) {
size_t link_offset;
string16 display_text =
delegate->GetMessageTextWithOffset(&link_offset);
string16 link_text = delegate->GetLinkText();
AddLabelAndLink(display_text, link_text, link_offset, 0,
G_CALLBACK(OnLinkClick));
gtk_widget_show_all(border_bin_.get());
}
private:
static void OnLinkClick(GtkWidget* button, LinkInfoBar* link_info_bar) {
if (link_info_bar->delegate_->AsLinkInfoBarDelegate()->
LinkClicked(gtk_util::DispositionForCurrentButtonPressEvent())) {
link_info_bar->RemoveInfoBar();
}
}
};
// ConfirmInfoBar --------------------------------------------------------------
class ConfirmInfoBar : public InfoBar {
public:
explicit ConfirmInfoBar(ConfirmInfoBarDelegate* delegate)
: InfoBar(delegate) {
AddConfirmButton(ConfirmInfoBarDelegate::BUTTON_CANCEL);
AddConfirmButton(ConfirmInfoBarDelegate::BUTTON_OK);
string16 display_text = delegate->GetMessageText();
string16 link_text = delegate->GetLinkText();
AddLabelAndLink(display_text, link_text, display_text.size(),
kElementPadding, G_CALLBACK(OnLinkClick));
gtk_widget_show_all(border_bin_.get());
}
private:
// Adds a button to the info bar by type. It will do nothing if the delegate
// doesn't specify a button of the given type.
void AddConfirmButton(ConfirmInfoBarDelegate::InfoBarButton type) {
if (delegate_->AsConfirmInfoBarDelegate()->GetButtons() & type) {
GtkWidget* button = gtk_button_new_with_label(UTF16ToUTF8(
delegate_->AsConfirmInfoBarDelegate()->GetButtonLabel(type)).c_str());
gtk_util::CenterWidgetInHBox(hbox_, button, true, 0);
g_signal_connect(button, "clicked",
G_CALLBACK(type == ConfirmInfoBarDelegate::BUTTON_OK ?
OnOkButton : OnCancelButton),
this);
}
}
static void OnCancelButton(GtkWidget* button, ConfirmInfoBar* info_bar) {
if (info_bar->delegate_->AsConfirmInfoBarDelegate()->Cancel())
info_bar->RemoveInfoBar();
}
static void OnOkButton(GtkWidget* button, ConfirmInfoBar* info_bar) {
if (info_bar->delegate_->AsConfirmInfoBarDelegate()->Accept())
info_bar->RemoveInfoBar();
}
static void OnLinkClick(GtkWidget* button, ConfirmInfoBar* link_info_bar) {
if (link_info_bar->delegate_->AsConfirmInfoBarDelegate()->
LinkClicked(gtk_util::DispositionForCurrentButtonPressEvent())) {
link_info_bar->RemoveInfoBar();
}
}
};
// AlertInfoBarDelegate, InfoBarDelegate overrides: ----------------------------
InfoBar* AlertInfoBarDelegate::CreateInfoBar() {
return new AlertInfoBar(this);
}
// LinkInfoBarDelegate, InfoBarDelegate overrides: -----------------------------
InfoBar* LinkInfoBarDelegate::CreateInfoBar() {
return new LinkInfoBar(this);
}
// ConfirmInfoBarDelegate, InfoBarDelegate overrides: --------------------------
InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() {
return new ConfirmInfoBar(this);
}
|