tangxu
2024-10-14 0f58c0039852f4be3d449906e9f2fb54332126c6
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
// *********************************
// Message from Original Author:
//
// 2008 Jose Menendez Poo
// Please give me credit if you use this code. It's all I ask.
// Contact me for more info: menendezpoo@gmail.com
// *********************************
//
// Original project from http://ribbon.codeplex.com/
// Continue to support and maintain by http://officeribbon.codeplex.com/
 
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
 
namespace System.Windows.Forms
{
    public class RibbonCheckBox
        : RibbonItem
    {
        public enum CheckBoxOrientationEnum
        {
            Left = 0,
            Right = 1
        }
        public enum CheckBoxStyle
        {
            CheckBox,
            RadioButton
        }
 
        #region Fields
        private const int spacing = 3;
        private Rectangle _labelBounds;
        private Rectangle _checkboxBounds;
        private int _labelWidth;
        private int _checkboxSize;
        private CheckBoxOrientationEnum _checkBoxOrientation;
        private CheckBoxStyle _style;
 
        #endregion
 
        #region Events
 
        /// <summary>
        /// Raised when the <see cref="System.Windows.Forms.CheckBox.Checked"/> property value has changed
        /// </summary>
        public event EventHandler CheckBoxCheckChanged;
        /// <summary>
        /// Raised when the <see cref="System.Windows.Forms.CheckBox.Checked"/> property value is changing
        /// </summary>
        public event CancelEventHandler CheckBoxCheckChanging;
 
        #endregion
 
        #region Ctor
 
        public RibbonCheckBox()
        {
            _checkboxSize = 16;
        }
 
        #endregion
 
        #region Props
        /// <summary>
        /// Gets or sets the style of the checkbox item
        /// </summary>
        [DefaultValue(CheckBoxStyle.CheckBox)]
        [Category("Appearance")]
        public CheckBoxStyle Style
        {
            get => _style;
            set { _style = value; NotifyOwnerRegionsChanged(); }
        }
 
        /// <summary>
        /// Gets or sets the width of the Label
        /// </summary>
        [DefaultValue(CheckBoxOrientationEnum.Left)]
        [Category("Appearance")]
        public CheckBoxOrientationEnum CheckBoxOrientation
        {
            get => _checkBoxOrientation;
            set { _checkBoxOrientation = value; NotifyOwnerRegionsChanged(); }
        }
 
        /// <summary>
        /// Gets the bounds of the label that is shown next to the textbox
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual Rectangle LabelBounds => _labelBounds;
 
        /// <summary>
        /// Gets a value indicating if the label is currently visible
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool LabelVisible { get; private set; } = true;
 
        /// <summary>
        /// Gets the bounds of the text
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual Rectangle CheckBoxBounds => _checkboxBounds;
 
        /// <summary>
        /// Gets or sets the width of the Label
        /// </summary>
        [DefaultValue(0)]
        [Category("Appearance")]
        public int LabelWidth
        {
            get => _labelWidth;
            set { _labelWidth = value; NotifyOwnerRegionsChanged(); }
        }
 
        /// <summary>
        /// Gets or sets if a click on the text changes the checked state.
        /// </summary>
        [Description("Click on text changes the checked state.")]
        [Category("Behavior")]
        [DefaultValue(true)]
        public bool TextClickable { get; set; } = true;
 
        #endregion
 
        #region Methods
 
        /// <summary>
        /// Measures the suposed height of the boxbox
        /// </summary>
        /// <returns></returns>
        protected virtual int MeasureHeight()
        {
            return _checkboxSize + Owner.ItemMargin.Vertical;
        }
 
        public override void OnPaint(object sender, RibbonElementPaintEventArgs e)
        {
            if (Owner != null)
            {
                Owner.Renderer.OnRenderRibbonItem(new RibbonItemRenderEventArgs(Owner, e.Graphics, Bounds, this));
 
                if (Style == CheckBoxStyle.CheckBox)
                {
                    CheckBoxState CheckState = Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal;
                    if (Selected)
                        CheckState += 1;
 
                    if (CheckBoxOrientation == CheckBoxOrientationEnum.Left)
                        CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(_checkboxBounds.Left, _checkboxBounds.Top), CheckState);
                    else
                        CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(_checkboxBounds.Left + spacing, _checkboxBounds.Top), CheckState);
                }
                else
                {
                    RadioButtonState RadioState = Checked ? RadioButtonState.CheckedNormal : RadioButtonState.UncheckedNormal;
                    if (Selected)
                        RadioState += 1;
                    if (CheckBoxOrientation == CheckBoxOrientationEnum.Left)
                        RadioButtonRenderer.DrawRadioButton(e.Graphics, new Point(_checkboxBounds.Left, _checkboxBounds.Top), RadioState);
                    else
                        RadioButtonRenderer.DrawRadioButton(e.Graphics, new Point(_checkboxBounds.Left + spacing, _checkboxBounds.Top), RadioState);
                }
 
                if (LabelVisible)
                {
                    StringFormat f = new StringFormat();
                    if (_checkBoxOrientation == CheckBoxOrientationEnum.Left)
                        f.Alignment = StringAlignment.Near;
                    else
                        f.Alignment = StringAlignment.Far;
 
                    f.LineAlignment = StringAlignment.Far; //Top
                    f.Trimming = StringTrimming.None;
                    f.FormatFlags |= StringFormatFlags.NoWrap;
                    Owner.Renderer.OnRenderRibbonItemText(new RibbonTextEventArgs(Owner, e.Graphics, _labelBounds, this, LabelBounds, Text, f));
                }
            }
        }
 
        public override void SetBounds(Rectangle bounds)
        {
            base.SetBounds(bounds);
            if (CheckBoxOrientation == CheckBoxOrientationEnum.Left)
            {
                _checkboxBounds = new Rectangle(
                    bounds.Left + Owner.ItemMargin.Left,
                    bounds.Top + Owner.ItemMargin.Top + ((bounds.Height - _checkboxSize) / 2),
                    _checkboxSize,
                    _checkboxSize);
 
                int itemImageToTextSpacing = (SizeMode == RibbonElementSizeMode.DropDown) ? Owner.ItemImageToTextSpacing : 0;
                _labelBounds = Rectangle.FromLTRB(
                    _checkboxBounds.Right + itemImageToTextSpacing,
                    bounds.Top + Owner.ItemMargin.Top,
                    bounds.Right - Owner.ItemMargin.Right,
                    bounds.Bottom - Owner.ItemMargin.Bottom);
            }
            else
            {
                _checkboxBounds = new Rectangle(
                    bounds.Right - Owner.ItemMargin.Right - _checkboxSize,
                    bounds.Top + Owner.ItemMargin.Top + ((bounds.Height - _checkboxSize) / 2),
                    _checkboxSize,
                    _checkboxSize);
 
                _labelBounds = Rectangle.FromLTRB(
                    bounds.Left + Owner.ItemMargin.Left,
                    bounds.Top + Owner.ItemMargin.Top,
                    _checkboxBounds.Left,
                    bounds.Bottom - Owner.ItemMargin.Bottom);
            }
 
            if (SizeMode == RibbonElementSizeMode.Large)
            {
                LabelVisible = true;
            }
            else if (SizeMode == RibbonElementSizeMode.Medium)
            {
                LabelVisible = true;
                _labelBounds = Rectangle.Empty;
            }
            else if (SizeMode == RibbonElementSizeMode.Compact)
            {
                _labelBounds = Rectangle.Empty;
                LabelVisible = false;
            }
        }
 
        private bool checkedGlyphSize;
        public override Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e)
        {
            if (!checkedGlyphSize)
            {
                try
                {
                    if (Style == CheckBoxStyle.CheckBox)
                        _checkboxSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal).Height + spacing;
                    else
                        _checkboxSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal).Height + spacing;
                }
                catch { /* I don't mind at all */ }
                checkedGlyphSize = true;
            }
 
            if (!Visible && !Owner.IsDesignMode())
            {
                SetLastMeasuredSize(new Size(0, 0));
                return LastMeasuredSize;
            }
 
            Size size = Size.Empty;
 
            int w = Owner.ItemMargin.Horizontal;
            int iwidth = Image != null ? Image.Width + spacing : 0;
            int lwidth = string.IsNullOrEmpty(Text) ? 0 : _labelWidth > 0 ? _labelWidth : e.Graphics.MeasureString(Text, Owner.Font).ToSize().Width;
 
            w += _checkboxSize;
 
            switch (e.SizeMode)
            {
                case RibbonElementSizeMode.DropDown:
                    w += iwidth + lwidth + Owner.ItemImageToTextSpacing;
                    break;
                case RibbonElementSizeMode.Large:
                    w += iwidth + lwidth;
                    break;
                case RibbonElementSizeMode.Medium:
                    w += iwidth;
                    break;
            }
 
            SetLastMeasuredSize(new Size(w, MeasureHeight()));
 
            return LastMeasuredSize;
        }
 
        public override void OnMouseEnter(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseEnter(e);
 
            //Canvas.Cursor = Cursors.Default;
            //SetSelected(true);
        }
 
        public override void OnMouseLeave(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseLeave(e);
 
            Canvas.Cursor = Cursors.Default;
            //SetSelected(false);
        }
 
        public override void OnMouseDown(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseDown(e);
 
            Rectangle clickableBounds = TextClickable ? Bounds : CheckBoxBounds;
            if (clickableBounds.Contains(e.X, e.Y))
            {
                CancelEventArgs cev = new CancelEventArgs();
                OnCheckChanging(cev);
                if (!cev.Cancel)
                {
                    Checked = !Checked;
                    OnCheckChanged(e);
                }
            }
        }
 
        public override void OnMouseMove(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseMove(e);
 
            Rectangle clickableBounds = TextClickable ? Bounds : CheckBoxBounds;
            if (clickableBounds.Contains(e.X, e.Y))
            {
                Debug.WriteLine("Owner.Cursor = Cursors.Hand e.X=" + e.X + " e.Y=" + e.Y + " CheckBoxBounds (" + CheckBoxBounds + ")");
                Canvas.Cursor = Cursors.Hand;
 
                if (!Selected)
                    SetSelected(true);
            }
            else
            {
                Debug.WriteLine("Owner.Cursor = Cursors.Default e.X=" + e.X + " e.Y=" + e.Y + " CheckBoxBounds (" + CheckBoxBounds + ")");
                Canvas.Cursor = Cursors.Default;
 
                if (Selected)
                    SetSelected(false);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="CheckBoxCheckChanged"/> event
        /// </summary>
        /// <param name="e"></param>
        public void OnCheckChanged(EventArgs e)
        {
            if (!Enabled) return;
 
            NotifyOwnerRegionsChanged();
 
            if (CheckBoxCheckChanged != null)
            {
                CheckBoxCheckChanged(this, e);
            }
        }
        /// <summary>
        /// Raises the <see cref="CheckBoxCheckChanging"/> event
        /// </summary>
        /// <param name="e"></param>
        public void OnCheckChanging(CancelEventArgs e)
        {
            if (!Enabled) return;
 
            if (CheckBoxCheckChanging != null)
            {
                CheckBoxCheckChanging(this, e);
            }
        }
 
        #endregion
    }
}