-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessToolBar.xaml.cs
323 lines (296 loc) · 14.2 KB
/
ChessToolBar.xaml.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.ComponentModel;
namespace SrcChess2 {
/// <summary>
/// Toolbar for the Chess Program
/// </summary>
public partial class ChessToolBar {
/// <summary>
/// Class Ctor
/// </summary>
public ChessToolBar() {
InitializeComponent();
ProgressBar.Visibility = Visibility.Hidden;
}
/// <summary>
/// Start the progress bar
/// </summary>
public void StartProgressBar() {
ProgressBar.Visibility = Visibility.Visible;
ProgressBar.Start();
}
/// <summary>
/// Stop the progress bar
/// </summary>
public void EndProgressBar() {
ProgressBar.Stop();
ProgressBar.Visibility = Visibility.Hidden;
}
}
/// <summary>
/// Defines a toolbar button
/// </summary>
public class ToolBarButton : Button {
/// <summary>Image dependency property</summary>
public static readonly DependencyProperty ImageProperty;
/// <summary>Image Disabled dependency property</summary>
public static readonly DependencyProperty DisabledImageProperty;
/// <summary>Flip dependency property</summary>
public static readonly DependencyProperty FlipProperty;
/// <summary>Image dependency property</summary>
public static readonly DependencyProperty TextProperty;
/// <summary>DisplayStyle dependency property</summary>
public static readonly DependencyProperty DisplayStyleProperty;
/// <summary>Inner Image control</summary>
private Image? m_imageCtrl;
/// <summary>Inner Text control</summary>
private TextBlock? m_textCtrl;
/// <summary>Display Style applied to the Toolbarbutton</summary>
public enum TbDisplayStyle {
/// <summary>Image only displayed</summary>
Image,
/// <summary>Text only displayed</summary>
Text,
/// <summary>Image and Text displayed</summary>
ImageAndText
}
/// <summary>
/// Class ctor
/// </summary>
static ToolBarButton() {
ImageProperty = DependencyProperty.Register("Image",
typeof(ImageSource),
typeof(ToolBarButton),
new FrameworkPropertyMetadata(defaultValue: null,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
ImageChanged));
DisabledImageProperty = DependencyProperty.Register("DisabledImage",
typeof(ImageSource),
typeof(ToolBarButton),
new FrameworkPropertyMetadata(defaultValue: null,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
DisabledImageChanged));
FlipProperty = DependencyProperty.Register("Flip",
typeof(bool),
typeof(ToolBarButton),
new FrameworkPropertyMetadata(defaultValue: false,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
FlipChanged));
TextProperty = DependencyProperty.Register("Text",
typeof(string),
typeof(ToolBarButton),
new FrameworkPropertyMetadata(defaultValue: "",
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
TextChanged));
DisplayStyleProperty = DependencyProperty.RegisterAttached("DisplayStyle",
typeof(TbDisplayStyle),
typeof(ToolBarButton),
new FrameworkPropertyMetadata(defaultValue: TbDisplayStyle.Text,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsParentArrange | FrameworkPropertyMetadataOptions.AffectsParentMeasure | FrameworkPropertyMetadataOptions.Inherits,
DisplayStyleChanged));
IsEnabledProperty.OverrideMetadata(typeof(ToolBarButton), new FrameworkPropertyMetadata(defaultValue: true, new PropertyChangedCallback(IsEnabledChanged)));
}
/// <summary>
/// Class constructor
/// </summary>
public ToolBarButton() : base() {
Style = new Style(typeof(ToolBarButton), (Style)FindResource(ToolBar.ButtonStyleKey));
BuildInnerButton();
}
/// <summary>
/// Called when Image property changed
/// </summary>
private static void ImageChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj is ToolBarButton me && e.OldValue != e.NewValue) {
me.UpdateInnerButton();
}
}
/// <summary>
/// Image displayed to the button
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
[Bindable(true)]
[Category("Layout")]
[Description("Image displayed in button")]
public ImageSource Image {
get => (ImageSource)GetValue(ImageProperty);
set => SetValue(ImageProperty, value);
}
/// <summary>
/// Called when Disabled Image property changed
/// </summary>
private static void DisabledImageChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj is ToolBarButton me && e.OldValue != e.NewValue) {
me.UpdateInnerButton();
}
}
/// <summary>
/// Disabled Image displayed to the button
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
[Bindable(true)]
[Category("Layout")]
[Description("Disabled Image displayed in button")]
public ImageSource DisabledImage {
get => (ImageSource)GetValue(DisabledImageProperty);
set => SetValue(DisabledImageProperty, value);
}
/// <summary>
/// Called when Flip property changed
/// </summary>
private static void FlipChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj is ToolBarButton me && e.OldValue != e.NewValue) {
me.UpdateInnerButton();
}
}
/// <summary>
/// Flip the image horizontally
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
[Bindable(true)]
[Category("Layout")]
[Description("Flip horizontally the Image displayed in button")]
public bool Flip {
get => (bool)GetValue(FlipProperty);
set => SetValue(FlipProperty, value);
}
/// <summary>
/// Called when Text property changed
/// </summary>
private static void TextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj is ToolBarButton me && e.OldValue != e.NewValue) {
me.UpdateInnerButton();
}
}
/// <summary>
/// Text displayed in button
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
[Bindable(true)]
[Category("Layout")]
[Description("Text displayed in button")]
public string Text {
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
/// <summary>
/// Called when DisplayStyle property changed
/// </summary>
private static void DisplayStyleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj is ToolBarButton tbItem && e.OldValue != e.NewValue) {
tbItem.UpdateInnerButton();
}
}
/// <summary>
/// Display Style applied to the button
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
[Bindable(true)]
[Category("Layout")]
[Description("Display Style applied to the button")]
public TbDisplayStyle DisplayStyle {
get => (TbDisplayStyle)GetValue(DisplayStyleProperty);
set => SetValue(DisplayStyleProperty, value);
}
/// <summary>
/// Set the Display Style
/// </summary>
/// <param name="element"> Dependency element</param>
/// <param name="displayStyle"> Display Style</param>
public static void SetDisplayStyle(DependencyObject element, TbDisplayStyle displayStyle) {
if (element == null) {
throw new ArgumentNullException(nameof(element));
}
element.SetValue(DisplayStyleProperty, displayStyle);
}
/// <summary>
/// Get the full name of the field attached to a column
/// </summary>
/// <param name="element"> Dependency element</param>
/// <returns>
/// Field full name
/// </returns>
public static TbDisplayStyle GetDisplayStyle(DependencyObject element) {
if (element == null) {
throw new ArgumentNullException(nameof(element));
}
return (TbDisplayStyle)element.GetValue(DisplayStyleProperty);
}
/// <summary>
/// Called when IsEnabled property changed
/// </summary>
private new static void IsEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj is ToolBarButton me && e.OldValue != e.NewValue) {
me.UpdateInnerButton();
}
}
/// <summary>
/// Set the source image depending the enabled state
/// </summary>
/// <param name="bFlip"> true if flipped</param>
private void SetImage(bool bFlip) {
ScaleTransform scaleTransform;
m_imageCtrl!.Source = (IsEnabled) ? Image : DisabledImage;
m_imageCtrl.OpacityMask = null;
if (bFlip) {
m_imageCtrl.RenderTransformOrigin = new Point(0.5, 0.5);
scaleTransform = new ScaleTransform {
ScaleX = -1
};
m_imageCtrl.RenderTransform = scaleTransform;
}
}
/// <summary>
/// Builds the inner controls to make the button
/// </summary>
private void BuildInnerButton() {
Grid grid;
grid = new Grid {
HorizontalAlignment = HorizontalAlignment.Center
};
m_imageCtrl = new Image();
m_textCtrl = new TextBlock() { Margin = new Thickness(5, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
grid.RowDefinitions.Add(new RowDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
Grid.SetColumn(m_imageCtrl, 0);
grid.Children.Add(m_imageCtrl);
Grid.SetColumn(m_textCtrl, 1);
grid.Children.Add(m_textCtrl);
Content = grid;
}
/// <summary>
/// Updates the inner controls of the button
/// </summary>
private void UpdateInnerButton() {
TbDisplayStyle displayStyle;
Grid grid;
string strText;
grid = (Grid)Content;
displayStyle = DisplayStyle;
strText = Text;
if (Image != null && (displayStyle == TbDisplayStyle.Image || displayStyle == TbDisplayStyle.ImageAndText)) {
grid.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
SetImage(Flip);
} else {
m_imageCtrl!.Source = null;
grid.ColumnDefinitions[0].Width = new GridLength(0);
}
if (!string.IsNullOrEmpty(strText) && (displayStyle == TbDisplayStyle.Text || displayStyle == TbDisplayStyle.ImageAndText)) {
grid.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);
m_textCtrl!.Text = strText;
} else {
m_textCtrl!.Text = string.Empty;
grid.ColumnDefinitions[1].Width = new GridLength(0);
}
}
}
}