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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
// 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/wizard/wizard.h"
#include <math.h>
#include "base/logging.h"
#include "chrome/browser/wizard/wizard_step.h"
#include "chrome/browser/standard_layout.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/resource_bundle.h"
#include "chrome/common/stl_util-inl.h"
#include "chrome/views/accelerator.h"
#include "chrome/views/label.h"
#include "chrome/views/native_button.h"
#include "generated_resources.h"
#include "SkBitmap.h"
static const int kMinButtonWidth = 100;
static const int kWizardWidth = 400;
static const int kWizardHeight = 300;
////////////////////////////////////////////////////////////////////////////////
//
// WizardView is the Wizard top level view
//
////////////////////////////////////////////////////////////////////////////////
class WizardView : public ChromeViews::View,
public ChromeViews::NativeButton::Listener {
public:
WizardView(Wizard* owner)
: owner_(owner),
contents_(NULL),
custom_navigation_descriptor_(NULL) {
title_ = new ChromeViews::Label(L"");
title_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
title_->SetFont(
ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::LargeFont));
AddChildView(title_);
cancel_ =
new ChromeViews::NativeButton(l10n_util::GetString(IDS_WIZARD_CANCEL));
AddChildView(cancel_);
cancel_->SetListener(this);
ChromeViews::Accelerator accelerator(VK_ESCAPE, false, false, false);
cancel_->AddAccelerator(accelerator);
previous_ = new ChromeViews::NativeButton(
l10n_util::GetString(IDS_WIZARD_PREVIOUS));
AddChildView(previous_);
previous_->SetListener(this);
next_ = new ChromeViews::NativeButton(l10n_util::GetString(IDS_WIZARD_NEXT));
AddChildView(next_);
next_->SetListener(this);
}
virtual ~WizardView() {
// Views provided by WizardStep instances are owned by the step
if (contents_) {
RemoveChildView(contents_);
contents_ = NULL;
}
// Sub views are deleted by the view system
}
void ComputeButtonSize(int* width, int *height) {
*width = kMinButtonWidth;
CSize s;
cancel_->GetPreferredSize(&s);
*height = s.cy;
*width = std::max(*width, static_cast<int>(s.cx));
previous_->GetPreferredSize(&s);
*width = std::max(*width, static_cast<int>(s.cx));
*height = std::max(*height, static_cast<int>(s.cy));
next_->GetPreferredSize(&s);
*width = std::max(*width, static_cast<int>(s.cx));
*height = std::max(*height, static_cast<int>(s.cy));
}
virtual void Layout() {
View* parent = GetParent();
DCHECK(parent);
if (!parent)
return;
if (title_->GetText().empty()) {
title_->SetBounds(0, 0, 0, 0);
title_->SetVisible(false);
} else {
CSize s;
title_->SetVisible(true);
title_->GetPreferredSize(&s);
title_->SetBounds(kPanelHorizMargin, kPanelVertMargin,
GetWidth() - (2 * kPanelVertMargin),
s.cy);
}
int bw, bh;
ComputeButtonSize(&bw, &bh);
int button_y = GetHeight() - kPanelVertMargin - bh;
cancel_->SetBounds(kPanelHorizMargin, button_y, bw, bh);
next_->SetBounds(GetWidth() - kPanelHorizMargin - bw, button_y, bw, bh);
previous_->SetBounds(next_->GetX() - kPanelHorizMargin - bw, button_y, bw, bh);
if (contents_) {
int y = title_->GetY() + title_->GetHeight() + kPanelVertMargin;
contents_->SetBounds(kPanelHorizMargin, y,
GetWidth() - (2 * kPanelHorizMargin),
cancel_->GetY() - kPanelVertMargin - y);
contents_->Layout();
}
}
virtual void GetPreferredSize(CSize* out) {
CSize s;
int w = 0, h = 0;
if (contents_) {
int extra_margin = (2 * kPanelVertMargin);
contents_->GetPreferredSize(&s);
w = s.cx;
h = s.cy + extra_margin;
}
if (!title_->GetText().empty()) {
title_->GetPreferredSize(&s);
w = std::max(w, static_cast<int>(s.cx));
h += s.cy;
}
int bw, bh;
ComputeButtonSize(&bw, &bh);
h += bh;
w += (2 * kPanelHorizMargin);
h += (2 * kPanelVertMargin);
out->cx = std::max(kWizardWidth, w);
out->cy = std::max(kWizardHeight, h);
}
virtual void ButtonPressed(ChromeViews::NativeButton *sender) {
if (sender == cancel_)
owner_->Cancel();
else if (sender == next_)
owner_->SelectNextStep();
else if (sender == previous_)
owner_->SelectPreviousStep();
}
typedef enum {
FIRST_STEP_STYLE,
NORMAL_STEP_STYLE,
LAST_STEP_STYLE,
CUSTOM_STYLE
} ContentsStyle;
void SetContents(ChromeViews::View* v, ContentsStyle s) {
if (contents_)
RemoveChildView(contents_);
custom_navigation_descriptor_ = NULL;
contents_ = v;
// Note: we change the navigation button only if a view is provided.
// if |v| is NULL, this wizard is closing and updating anything may
// cause a flash.
if (contents_) {
switch (s) {
case FIRST_STEP_STYLE:
previous_->SetVisible(false);
next_->SetVisible(true);
next_->SetLabel(l10n_util::GetString(IDS_WIZARD_NEXT));
break;
case NORMAL_STEP_STYLE:
previous_->SetVisible(true);
next_->SetVisible(true);
next_->SetLabel(l10n_util::GetString(IDS_WIZARD_NEXT));
break;
case LAST_STEP_STYLE:
previous_->SetVisible(true);
next_->SetVisible(true);
next_->SetLabel(l10n_util::GetString(IDS_WIZARD_DONE));
break;
}
AddChildView(contents_);
cancel_->SetVisible(true);
// Reset the button labels to the default values.
previous_->SetLabel(l10n_util::GetString(IDS_WIZARD_PREVIOUS));
next_->SetLabel(l10n_util::GetString(IDS_WIZARD_NEXT));
}
}
void SetTitle(const std::wstring& title) {
title_->SetText(title);
}
void NavigationDescriptorChanged() {
if (custom_navigation_descriptor_) {
SetCustomNavigationDescriptor(custom_navigation_descriptor_);
}
}
void SetCustomNavigationDescriptor(WizardNavigationDescriptor* wnd) {
custom_navigation_descriptor_ = wnd;
if (wnd->custom_default_button_) {
if (wnd->default_label_.empty()) {
next_->SetVisible(false);
} else {
next_->SetVisible(true);
next_->SetLabel(wnd->default_label_);
}
}
if (wnd->custom_alternate_button_) {
if (wnd->alternate_label_.empty()) {
previous_->SetVisible(false);
} else {
previous_->SetVisible(true);
previous_->SetLabel(wnd->alternate_label_);
}
}
cancel_->SetVisible(wnd->can_cancel_);
}
WizardNavigationDescriptor* GetCustomNavigationDescriptor() {
return custom_navigation_descriptor_;
}
void EnableNextButton(bool f) {
if (next_)
next_->SetEnabled(f);
}
bool IsNextButtonEnabled() {
if (next_)
return next_->IsEnabled();
else
return false;
}
void EnablePreviousButton(bool f) {
if (previous_)
previous_->SetEnabled(f);
}
bool IsPreviousButtonEnabled() {
if (previous_)
return previous_->IsEnabled();
else
return false;
}
private:
ChromeViews::Label* title_;
ChromeViews::NativeButton* next_;
ChromeViews::NativeButton* previous_;
ChromeViews::NativeButton* cancel_;
ChromeViews::View* contents_;
WizardNavigationDescriptor* custom_navigation_descriptor_;
Wizard* owner_;
DISALLOW_EVIL_CONSTRUCTORS(WizardView);
};
////////////////////////////////////////////////////////////////////////////////
//
// Wizard implementation
//
////////////////////////////////////////////////////////////////////////////////
Wizard::Wizard(WizardDelegate* d) : delegate_(d),
view_(NULL),
state_(NULL),
selected_step_(-1),
is_running_(false) {
}
Wizard::~Wizard() {
Abort();
delete view_;
RemoveAllSteps();
delete state_;
STLDeleteContainerPairSecondPointers(images_.begin(), images_.end());
}
void Wizard::SetState(DictionaryValue* state) {
delete state_;
state_ = state;
}
DictionaryValue* Wizard::GetState() {
return state_;
}
void Wizard::AddStep(WizardStep* s) {
steps_.push_back(s);
}
int Wizard::GetStepCount() const {
return static_cast<int>(steps_.size());
}
WizardStep* Wizard::GetStepAt(int index) {
DCHECK(index >= 0 && index < static_cast<int>(steps_.size()));
return steps_[index];
}
void Wizard::RemoveAllSteps() {
DCHECK(!is_running_);
int c = static_cast<int>(steps_.size());
while (--c >= 0)
steps_[c]->Dispose();
steps_.clear();
}
ChromeViews::View* Wizard::GetTopLevelView() {
if (!view_) {
view_ = new WizardView(this);
// We own the view and it should never be deleted by the parent
// view
view_->SetParentOwned(false);
}
return view_;
}
void Wizard::Start() {
DCHECK(view_ && view_->GetParent());
DCHECK(steps_.size() > 0);
DCHECK(!is_running_);
is_running_ = true;
SelectStepAt(0);
view_->Layout();
}
void Wizard::Reset() {
is_running_ = false;
if (selected_step_ != -1)
steps_[selected_step_]->WillBecomeInvisible(this,
WizardStep::CANCEL_ACTION);
selected_step_ = -1;
view_->EnableNextButton(true);
view_->EnablePreviousButton(true);
view_->SetContents(NULL, WizardView::NORMAL_STEP_STYLE);
if (view_->GetParent())
view_->GetParent()->RemoveChildView(view_);
}
void Wizard::WizardDone(bool commit) {
if (is_running_) {
Reset();
is_running_ = false;
if (delegate_)
delegate_->WizardClosed(commit);
}
}
void Wizard::Abort() {
WizardDone(false);
}
// Note: this private method doesn't call WillBecomeInvisible because
// it needs to be called before deciding what step to select next.
void Wizard::SelectStepAt(int index) {
DCHECK(index >= 0 && index < static_cast<int>(steps_.size()));
if (index == selected_step_)
return;
selected_step_ = index;
WizardView::ContentsStyle style = WizardView::NORMAL_STEP_STYLE;
if (selected_step_ == 0)
style = WizardView::FIRST_STEP_STYLE;
else if (selected_step_ == (static_cast<int>(steps_.size()) - 1))
style = WizardView::LAST_STEP_STYLE;
view_->SetContents(steps_[selected_step_]->GetView(this), style);
steps_[selected_step_]->DidBecomeVisible(this);
WizardNavigationDescriptor* wnd =
steps_[selected_step_]->GetNavigationDescriptor();
if (wnd)
view_->SetCustomNavigationDescriptor(wnd);
view_->SetTitle(steps_[selected_step_]->GetTitle(this));
CSize s;
view_->GetPreferredSize(&s);
if (view_->GetWidth() != s.cx || view_->GetHeight() != s.cy)
delegate_->ResizeTopLevelView(s.cx, s.cy);
view_->Layout();
view_->SchedulePaint();
}
void Wizard::NavigationDescriptorChanged() {
view_->NavigationDescriptorChanged();
}
void Wizard::Cancel() {
Abort();
}
void Wizard::SelectStep(bool is_forward) {
int delta = is_forward ? 1 : -1;
WizardNavigationDescriptor* wnd = view_->GetCustomNavigationDescriptor();
if (wnd) {
if (is_forward && wnd->custom_default_button_)
delta = wnd->default_offset_;
else if (!is_forward && wnd->custom_alternate_button_)
delta = wnd->alternate_offset_;
}
if (selected_step_ != -1) {
steps_[selected_step_]->WillBecomeInvisible(
this, is_forward ? WizardStep::DEFAULT_ACTION :
WizardStep::ALTERNATE_ACTION);
}
bool step_selected = false;
int i;
for (i = selected_step_ + delta;
i >= 0 && i < static_cast<int>(steps_.size()); i += delta) {
if (steps_[i]->IsEnabledFor(this)) {
SelectStepAt(i);
step_selected = true;
break;
}
}
if (!step_selected && is_forward) {
WizardDone(true);
}
}
void Wizard::SelectNextStep() {
SelectStep(true);
}
void Wizard::SelectPreviousStep() {
SelectStep(false);
}
void Wizard::SetImage(const std::wstring& image_name, SkBitmap* image) {
ImageMap::iterator i = images_.find(image_name);
if (i != images_.end())
delete i->second;
images_[image_name] = image;
}
SkBitmap* Wizard::GetImage(const std::wstring& image_name, bool remove_image) {
ImageMap::iterator i = images_.find(image_name);
SkBitmap* bitmap = NULL;
if (i != images_.end()) {
bitmap = i->second;
if (remove_image) {
images_.erase(i);
}
}
return bitmap;
}
void Wizard::EnableNextStep(bool flag) {
view_->EnableNextButton(flag);
}
bool Wizard::IsNextStepEnabled() const {
return view_->IsNextButtonEnabled();
}
void Wizard::EnablePreviousStep(bool flag) {
view_->EnablePreviousButton(flag);
}
bool Wizard::IsPreviousStepEnabled() const {
return view_->IsPreviousButtonEnabled();
}
|