-
Notifications
You must be signed in to change notification settings - Fork 0
/
WizardBase.cs
442 lines (371 loc) · 13.7 KB
/
WizardBase.cs
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
/*
© 2005, 2011 MAY-MART. All rights reserved.
The code is originally based on TSWizard library (http://www.codeproject.com/KB/dialog/tswizard.aspx).
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace MayMart.WinFormsWizard
{
[DefaultEvent("LoadSteps")]
public partial class WizardBase : Form
{
private Image _imageLogo;
private Image _imageSideBarImage;
private StepLayout _designerStepLayout;
//private AllowClose _allowClose = AllowClose.AlwaysAllow;
private string _firstStepKey;
private WizardStepBase _currentStep;
private readonly Dictionary<string, WizardStepBase> _steps;
[Browsable(true)]
[Category("Wizard")]
[Description("Gets or sets the image that is displayed in the upper-right corner of the wizard.")]
public Image Logo
{
get
{
return _imageLogo;
}
set
{
_imageLogo = value;
OnLogoChanged(null, EventArgs.Empty);
}
}
[Browsable(true)]
[Category("Wizard")]
[Description("Gets or sets the image that is displayed along the left side of the wizard, seen only on Exterior pages.")]
public Image SideBarImage
{
get
{
return _imageSideBarImage;
}
set
{
_imageSideBarImage = value;
OnSideBarImageChanged(null, EventArgs.Empty);
}
}
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Category("Design")]
[Description("Allows you to switch the design-time view from page layout to another.")]
public StepLayout StepLayout
{
get
{
return _designerStepLayout;
}
set
{
if (DesignMode)
{
_designerStepLayout = value;
UpdateLayout(_designerStepLayout);
}
}
}
public WizardBase()
{
InitializeComponent();
_steps = new Dictionary<string, WizardStepBase>();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!DesignMode)
{
ConfigureWizard();
if (string.IsNullOrEmpty(_firstStepKey))
throw new InvalidOperationException("Wizard's first step not set.");
ResetAllSteps();
GotoStepByKey(_firstStepKey, StepDirection.InitialStep);
}
else
UpdateLayout(_designerStepLayout);
}
protected virtual void ConfigureWizard()
{
// Nothing
}
public void RegisterStep<T>(T step, string name = null)
where T : WizardStepBase
{
if (step == null)
throw new ArgumentNullException("step");
string key = GetStepKey<T>(name);
if (_steps.ContainsKey(key))
throw new InvalidOperationException("The step already registered.");
_steps[key] = step;
step.Wizard = this;
}
public void SetFirstStep<T>(string name = null)
where T : WizardStepBase
{
_firstStepKey = GetStepKey<T>(name);
}
public WizardStepBase GetStep<T>(string name = null)
where T : WizardStepBase
{
return _steps[GetStepKey<T>(name)];
}
public static string GetStepKey<T>(string name = null)
where T : WizardStepBase
{
return string.Format("{0}${1}", typeof(T).Name, name);
}
public void ResetAllSteps()
{
foreach (var step in _steps.Values)
{
step.FireResetStepEvent();
}
}
public void GotoNext()
{
if (_currentStep.NextStepMode != NextStepMode.Next || _currentStep.NextStepEnabled == false)
throw new InvalidOperationException("Next step disabled.");
GotoStepByKey(_currentStep.NextStepKey, StepDirection.NextStep);
}
public void Finish()
{
if (_currentStep.NextStepMode != NextStepMode.Finish || _currentStep.NextStepEnabled == false)
throw new InvalidOperationException("Finish disabled.");
DialogResult = DialogResult.OK;
Close();
}
public void GotoBack()
{
GotoStepByKey(_currentStep.PreviousStepKey, StepDirection.PreviousStep);
}
public void GotoStep<T>(string name = null, StepDirection direction = StepDirection.Jump)
where T : WizardStepBase
{
GotoStepByKey(GetStepKey<T>(name), direction);
}
private bool _callingFromPreview;
private bool _haveBeenCalledFromPreview;
private void GotoStepByKey(string key, StepDirection direction)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
if (!_steps.ContainsKey(key))
throw new InvalidOperationException("The step is not registered.");
WizardStepBase step = _steps[key];
// Giving ability to preview current step change
if (!_callingFromPreview)
{
try
{
_callingFromPreview = true;
PreviewStepChange(_currentStep, step, direction);
}
finally
{
_callingFromPreview = false;
}
}
// Cancelling original step change if it was already changed in PreviewStepChange
if (!_callingFromPreview && _haveBeenCalledFromPreview)
{
_haveBeenCalledFromPreview = false;
return;
}
// Changing step
try
{
SuspendLayout();
try
{
if (_currentStep != null)
DetatchStep();
_currentStep = step;
AttachStep(direction);
}
finally
{
ResumeLayout(true);
}
}
finally
{
_haveBeenCalledFromPreview = _callingFromPreview;
}
}
protected virtual void PreviewStepChange(WizardStepBase stepFrom, WizardStepBase stepTo, StepDirection direction)
{
// Nothing
}
protected virtual void UpdateLayout(StepLayout layout)
{
if (!Enum.IsDefined(typeof(StepLayout), StepLayout))
throw new InvalidEnumArgumentException("layout", (int)layout, typeof(StepLayout));
SuspendLayout();
try
{
switch (layout)
{
case StepLayout.Interior:
wizardTop.Visible = true;
wizardTop.Dock = DockStyle.Top;
wizardTop.Height = 0x40;
topLine.Visible = true;
topLine.Dock = DockStyle.Top;
sidePanel.Visible = false;
panelStep.Dock = DockStyle.Fill;
panelStep.DockPadding.All = 8;
BackColor = SystemColors.Control;
panelStep.BackColor = SystemColors.Control;
break;
case StepLayout.Exterior:
wizardTop.Visible = false;
topLine.Visible = false;
panelStep.BackColor = Color.White;
panelStep.Dock = DockStyle.Fill;
sidePanel.Visible = true;
sidePanel.Dock = DockStyle.Left;
sidePanel.Width = 160;
break;
}
panelStep.BringToFront();
}
finally
{
ResumeLayout();
}
}
private void AttachStep(StepDirection direction)
{
_currentStep.StepTitleChanged += OnStepTitleChanged;
_currentStep.StepDescriptionChanged += OnStepDescriptionChanged;
_currentStep.NextStepChanged += OnNextStepChanged;
_currentStep.PreviousStepChanged += OnPreviousStepChanged;
_currentStep.CanBeCancelledChanged += OnCanBeCancelledChanged;
_currentStep.PageLayoutChanged += OnStepLayoutChanged;
_currentStep.LogoChanged += OnLogoChanged;
_currentStep.SideBarImageChanged += OnSideBarImageChanged;
OnStepTitleChanged(this, EventArgs.Empty);
OnStepDescriptionChanged(this, EventArgs.Empty);
_currentStep.Dock = DockStyle.Fill;
panelStep.Controls.Add(_currentStep);
OnNextStepChanged(this, EventArgs.Empty);
OnPreviousStepChanged(this, EventArgs.Empty);
OnCanBeCancelledChanged(this, EventArgs.Empty);
OnStepLayoutChanged(this, EventArgs.Empty);
OnLogoChanged(this, EventArgs.Empty);
OnSideBarImageChanged(this, EventArgs.Empty);
_currentStep.FireShowEvent(new ShowStepEventArgs(direction));
}
private void DetatchStep()
{
_currentStep.StepTitleChanged -= OnStepTitleChanged;
_currentStep.StepDescriptionChanged -= OnStepDescriptionChanged;
_currentStep.NextStepChanged -= OnNextStepChanged;
_currentStep.PreviousStepChanged -= OnPreviousStepChanged;
_currentStep.CanBeCancelledChanged -= OnCanBeCancelledChanged;
_currentStep.PageLayoutChanged -= OnStepLayoutChanged;
_currentStep.LogoChanged -= OnLogoChanged;
_currentStep.SideBarImageChanged -= OnSideBarImageChanged;
panelStep.Controls.Remove(_currentStep);
}
private void OnButtonNextClick(object sender, EventArgs e)
{
if (!_currentStep.NextStepEnabled)
return;
if (_currentStep.NextStepMode == NextStepMode.Next)
{
_currentStep.NextClicked();
}
else
_currentStep.FinishClicked();
}
private void OnButtonBackClick(object sender, EventArgs e)
{
_currentStep.BackClicked();
}
private void OnButtonCancelClick(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
if (!Modal)
Close();
}
//private bool AskToClose()
//{
// return (MessageBox.Show("Do you wish to quit the wizard now?\r\nYour changes won't be saved if you do", "Exit wizard?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes);
//}
protected override void OnClosing(CancelEventArgs e)
{
if (DialogResult == DialogResult.Cancel)
{
if (_currentStep.CanBeCancelled)
{
//if (AllowClose == AllowClose.Ask)
// e.Cancel = !AskToClose();
}
else
e.Cancel = true;
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (((_currentStep != null) && !_currentStep.CanBeCancelled) && (DialogResult == DialogResult.None))
{
e.Cancel = true;
}
else
{
if (_currentStep != null)
{
_currentStep.OnFormClosing(e);
}
base.OnFormClosing(e);
}
}
private void OnLogoChanged(object sender, EventArgs e)
{
if ((_currentStep == null) || (_currentStep.StepLogo == null))
{
pictureBoxLogo.Image = _imageLogo;
}
else
pictureBoxLogo.Image = _currentStep.StepLogo;
}
private void OnSideBarImageChanged(object sender, EventArgs e)
{
if ((_currentStep == null) || (_currentStep.SideBarImage == null))
{
sidePanel.BackgroundImage = _imageSideBarImage;
}
else
sidePanel.BackgroundImage = _currentStep.SideBarImage;
}
private void OnNextStepChanged(object sender, EventArgs e)
{
buttonNext.Text = _currentStep.NextStepMode == NextStepMode.Next ? "&Далее >" : "&Готово";
buttonNext.Enabled = _currentStep.NextStepEnabled;
}
private void OnPreviousStepChanged(object sender, EventArgs e)
{
buttonBack.Enabled = !string.IsNullOrEmpty(_currentStep.PreviousStepKey);
}
private void OnCanBeCancelledChanged(object sender, EventArgs e)
{
buttonCancel.Enabled = _currentStep.CanBeCancelled;
}
private void OnStepLayoutChanged(object sender, EventArgs e)
{
UpdateLayout(_currentStep.StepLayout);
}
private void OnStepTitleChanged(object o, EventArgs e)
{
labelTitle.Text = _currentStep.StepTitle;
}
private void OnStepDescriptionChanged(object o, EventArgs e)
{
labelDescription.Text = _currentStep.StepDescription;
}
}
}