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
|
// Copyright (c) 2006-2008 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/views/first_run_bubble.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/win_util.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/options_window.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "grit/theme_resources.h"
#include "views/event.h"
#include "views/controls/button/native_button.h"
#include "views/controls/button/image_button.h"
#include "views/controls/label.h"
#include "views/focus/focus_manager.h"
#include "views/standard_layout.h"
#include "views/window/window.h"
namespace {
// How much extra padding to put around our content over what the InfoBubble
// provides.
static const int kBubblePadding = 4;
// How much extra padding to put around our content over what the InfoBubble
// provides in alternative OEM bubble.
static const int kOEMBubblePadding = 4;
// Padding between parts of strings on the same line (for instance,
// "New!" and "Search from the address bar!"
static const int kStringSeparationPadding = 2;
// Margin around close button.
static const int kMarginRightOfCloseButton = 7;
std::wstring GetDefaultSearchEngineName(Profile* profile) {
if (!profile) {
NOTREACHED();
return std::wstring();
}
const TemplateURL* const default_provider =
profile->GetTemplateURLModel()->GetDefaultSearchProvider();
if (!default_provider) {
// TODO(cpu): bug 1187517. It is possible to have no default provider.
// returning an empty string is a stopgap measure for the crash
// http://code.google.com/p/chromium/issues/detail?id=2573
return std::wstring();
}
return default_provider->short_name();
}
} // namespace
// Base class for implementations of the client view which appears inside the
// first run bubble. It is a dialog-ish view, but is not a true dialog.
class FirstRunBubbleViewBase : public views::View,
public views::ButtonListener,
public views::FocusChangeListener {
public:
// Called by FirstRunBubble::Show to request focus for the proper button
// in the FirstRunBubbleView when it is shown.
virtual void BubbleShown() = 0;
};
class FirstRunBubbleView : public FirstRunBubbleViewBase {
public:
FirstRunBubbleView(FirstRunBubble* bubble_window, Profile* profile)
: bubble_window_(bubble_window),
label1_(NULL),
label2_(NULL),
label3_(NULL),
keep_button_(NULL),
change_button_(NULL) {
gfx::Font& font =
ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont);
label1_ = new views::Label(l10n_util::GetString(IDS_FR_BUBBLE_TITLE));
label1_->SetFont(font.DeriveFont(3, gfx::Font::BOLD));
label1_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
AddChildView(label1_);
gfx::Size ps = GetPreferredSize();
label2_ = new views::Label(l10n_util::GetString(IDS_FR_BUBBLE_SUBTEXT));
label2_->SetMultiLine(true);
label2_->SetFont(font);
label2_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
label2_->SizeToFit(ps.width() - kBubblePadding * 2);
AddChildView(label2_);
std::wstring question_str
= l10n_util::GetStringF(IDS_FR_BUBBLE_QUESTION,
GetDefaultSearchEngineName(profile));
label3_ = new views::Label(question_str);
label3_->SetMultiLine(true);
label3_->SetFont(font);
label3_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
label3_->SizeToFit(ps.width() - kBubblePadding * 2);
AddChildView(label3_);
std::wstring keep_str =
l10n_util::GetStringF(IDS_FR_BUBBLE_OK,
GetDefaultSearchEngineName(profile));
keep_button_ = new views::NativeButton(this, keep_str);
keep_button_->SetIsDefault(true);
AddChildView(keep_button_);
std::wstring change_str = l10n_util::GetString(IDS_FR_BUBBLE_CHANGE);
change_button_ = new views::NativeButton(this, change_str);
AddChildView(change_button_);
}
void BubbleShown() {
keep_button_->RequestFocus();
}
virtual void ButtonPressed(views::Button* sender) {
bubble_window_->Close();
if (change_button_ == sender) {
Browser* browser = BrowserList::GetLastActive();
if (browser) {
ShowOptionsWindow(OPTIONS_PAGE_GENERAL, OPTIONS_GROUP_DEFAULT_SEARCH,
browser->profile());
}
}
}
virtual void Layout() {
gfx::Size canvas = GetPreferredSize();
// The multiline business that follows is dirty hacks to get around
// bug 1325257.
label1_->SetMultiLine(false);
gfx::Size pref_size = label1_->GetPreferredSize();
label1_->SetMultiLine(true);
label1_->SizeToFit(canvas.width() - kBubblePadding * 2);
label1_->SetBounds(kBubblePadding, kBubblePadding,
canvas.width() - kBubblePadding * 2,
pref_size.height());
int next_v_space = label1_->y() + pref_size.height() +
kRelatedControlSmallVerticalSpacing;
pref_size = label2_->GetPreferredSize();
label2_->SetBounds(kBubblePadding, next_v_space,
canvas.width() - kBubblePadding * 2,
pref_size.height());
next_v_space = label2_->y() + label2_->height() +
kPanelSubVerticalSpacing;
pref_size = label3_->GetPreferredSize();
label3_->SetBounds(kBubblePadding, next_v_space,
canvas.width() - kBubblePadding * 2,
pref_size.height());
pref_size = change_button_->GetPreferredSize();
change_button_->SetBounds(
canvas.width() - pref_size.width() - kBubblePadding,
canvas.height() - pref_size.height() - kButtonVEdgeMargin,
pref_size.width(), pref_size.height());
pref_size = keep_button_->GetPreferredSize();
keep_button_->SetBounds(change_button_->x() - pref_size.width() -
kRelatedButtonHSpacing, change_button_->y(),
pref_size.width(), pref_size.height());
}
virtual gfx::Size GetPreferredSize() {
return gfx::Size(views::Window::GetLocalizedContentsSize(
IDS_FIRSTRUNBUBBLE_DIALOG_WIDTH_CHARS,
IDS_FIRSTRUNBUBBLE_DIALOG_HEIGHT_LINES));
}
virtual void FocusWillChange(View* focused_before, View* focused_now) {
if (focused_before && focused_before->GetClassName() ==
views::NativeButton::kViewClassName) {
views::NativeButton* before =
static_cast<views::NativeButton*>(focused_before);
before->SetIsDefault(false);
}
if (focused_now && focused_now->GetClassName() ==
views::NativeButton::kViewClassName) {
views::NativeButton* after =
static_cast<views::NativeButton*>(focused_now);
after->SetIsDefault(true);
}
}
private:
FirstRunBubble* bubble_window_;
views::Label* label1_;
views::Label* label2_;
views::Label* label3_;
views::NativeButton* change_button_;
views::NativeButton* keep_button_;
DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleView);
};
class FirstRunOEMBubbleView : public FirstRunBubbleViewBase {
public:
FirstRunOEMBubbleView(FirstRunBubble* bubble_window, Profile* profile)
: bubble_window_(bubble_window),
label1_(NULL),
label2_(NULL),
label3_(NULL),
close_button_(NULL) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
gfx::Font& font = rb.GetFont(ResourceBundle::MediumFont);
label1_ = new views::Label(
l10n_util::GetString(IDS_FR_OEM_BUBBLE_TITLE_1));
label1_->SetFont(font.DeriveFont(3, gfx::Font::BOLD));
label1_->SetColor(SK_ColorRED);
label1_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
AddChildView(label1_);
label2_ = new views::Label(
l10n_util::GetString(IDS_FR_OEM_BUBBLE_TITLE_2));
label2_->SetFont(font.DeriveFont(3, gfx::Font::BOLD));
label2_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
AddChildView(label2_);
gfx::Size ps = GetPreferredSize();
label3_ = new views::Label(
l10n_util::GetString(IDS_FR_OEM_BUBBLE_SUBTEXT));
label3_->SetMultiLine(true);
label3_->SetFont(font);
label3_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
label3_->SizeToFit(ps.width() - kOEMBubblePadding * 2);
AddChildView(label3_);
close_button_ = new views::ImageButton(this);
close_button_->SetImage(views::CustomButton::BS_NORMAL,
rb.GetBitmapNamed(IDR_CLOSE_BAR));
close_button_->SetImage(views::CustomButton::BS_HOT,
rb.GetBitmapNamed(IDR_CLOSE_BAR_H));
close_button_->SetImage(views::CustomButton::BS_PUSHED,
rb.GetBitmapNamed(IDR_CLOSE_BAR_P));
AddChildView(close_button_);
}
void BubbleShown() {
this->RequestFocus();
// No button in oem_bubble to request focus.
}
virtual void ButtonPressed(views::Button* sender) {
bubble_window_->Close();
}
virtual void Layout() {
gfx::Size canvas = GetPreferredSize();
// First, draw the close button on the far right.
gfx::Size sz = close_button_->GetPreferredSize();
close_button_->SetBounds(canvas.width() - sz.width() -
kMarginRightOfCloseButton,
kOEMBubblePadding,
sz.width(),
sz.height());
gfx::Size pref_size = label1_->GetPreferredSize();
label1_->SetBounds(kOEMBubblePadding, kOEMBubblePadding,
pref_size.width() + kOEMBubblePadding * 2,
pref_size.height());
pref_size = label2_->GetPreferredSize();
label2_->SetBounds(kOEMBubblePadding * 2 + label1_->
GetPreferredSize().width(),
kOEMBubblePadding,
canvas.width() - kOEMBubblePadding * 2,
pref_size.height());
int next_v_space = label1_->y() + pref_size.height() +
kRelatedControlSmallVerticalSpacing;
pref_size = label3_->GetPreferredSize();
label3_->SetBounds(kOEMBubblePadding, next_v_space,
canvas.width() - kOEMBubblePadding * 2,
pref_size.height());
}
virtual gfx::Size GetPreferredSize() {
return gfx::Size(views::Window::GetLocalizedContentsSize(
IDS_FIRSTRUNOEMBUBBLE_DIALOG_WIDTH_CHARS,
IDS_FIRSTRUNOEMBUBBLE_DIALOG_HEIGHT_LINES));
}
virtual void FocusWillChange(View* focused_before, View* focused_now) {
// No buttons in oem_bubble to register focus changes.
}
private:
FirstRunBubble* bubble_window_;
views::Label* label1_;
views::Label* label2_;
views::Label* label3_;
views::ImageButton* close_button_;
DISALLOW_COPY_AND_ASSIGN(FirstRunOEMBubbleView);
};
// Keep the bubble around for kLingerTime milliseconds, to prevent accidental
// closure.
static const int kLingerTime = 1000;
void FirstRunBubble::OnActivate(UINT action, BOOL minimized, HWND window) {
// We might get re-enabled right before we are closed (sequence is: we get
// deactivated, we call close, before we are actually closed we get
// reactivated). Don't do the disabling of the parent in such cases.
if (action == WA_ACTIVE && !has_been_activated_) {
has_been_activated_ = true;
::EnableWindow(GetParent(), false);
MessageLoop::current()->PostDelayedTask(FROM_HERE,
enable_window_method_factory_.NewRunnableMethod(
&FirstRunBubble::EnableParent),
kLingerTime);
}
// Keep window from automatically closing until kLingerTime has passed.
if (::IsWindowEnabled(GetParent()))
InfoBubble::OnActivate(action, minimized, window);
}
void FirstRunBubble::InfoBubbleClosing(InfoBubble* info_bubble,
bool closed_by_escape) {
// Make sure our parent window is re-enabled.
if (!IsWindowEnabled(GetParent()))
::EnableWindow(GetParent(), true);
enable_window_method_factory_.RevokeAll();
GetFocusManager()->RemoveFocusChangeListener(view_);
}
// static
FirstRunBubble* FirstRunBubble::Show(Profile* profile, views::Window* parent,
const gfx::Rect& position_relative_to,
bool use_OEM_bubble) {
FirstRunBubble* window = new FirstRunBubble();
FirstRunBubbleViewBase* view = NULL;
if (use_OEM_bubble)
view = new FirstRunOEMBubbleView(window, profile);
else
view = new FirstRunBubbleView(window, profile);
window->SetDelegate(window);
window->set_view(view);
window->Init(parent, position_relative_to, view);
window->ShowWindow(SW_SHOW);
window->GetFocusManager()->AddFocusChangeListener(view);
view->BubbleShown();
return window;
}
void FirstRunBubble::EnableParent() {
::EnableWindow(GetParent(), true);
// Reactivate the FirstRunBubble so it responds to OnActivate messages.
SetWindowPos(GetParent(), 0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOREDRAW | SWP_SHOWWINDOW);
}
|