tangxu
2024-10-14 6cd995b71dfc74d4d96347d0bc535fddf36fa9df
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
using System.ComponentModel;
using System.Drawing;
 
namespace System.Windows.Forms
{
    //[Designer(typeof(RibbonUpDown))]
    public class RibbonUpDown : RibbonTextBox
    {
        #region Fields
        private const int spacing = 3;
        //private Ribbon _ownerRibbon;
        private readonly int _UpDownSize = 16;
 
        #endregion
 
        #region Events
 
        public event MouseEventHandler UpButtonClicked;
        public event MouseEventHandler DownButtonClicked;
 
        #endregion
 
        #region Ctor
 
        public RibbonUpDown()
        {
            _textboxWidth = 50;
            _UpDownSize = 16;
        }
 
        #endregion
 
        #region Props
 
 
        #endregion
 
        #region Methods
 
        /// <summary>
        /// Measures the suposed height of the textbox
        /// </summary>
        /// <returns></returns>
        public override int MeasureHeight()
        {
            return 16 + Owner.ItemMargin.Vertical;
        }
 
        public override void OnPaint(object sender, RibbonElementPaintEventArgs e)
        {
            if (Owner == null) return;
 
            Owner.Renderer.OnRenderRibbonItem(new RibbonItemRenderEventArgs(Owner, e.Graphics, Bounds, this));
 
            if (ImageVisible)
                Owner.Renderer.OnRenderRibbonItemImage(new RibbonItemBoundsEventArgs(Owner, e.Graphics, e.Clip, this, _imageBounds));
 
            using (StringFormat f = StringFormatFactory.NearCenterNoWrap(StringTrimming.None))
            {
                f.Alignment = StringAlignment.Near;
                f.LineAlignment = StringAlignment.Center;
                f.Trimming = StringTrimming.None;
                f.FormatFlags |= StringFormatFlags.NoWrap;
 
                Owner.Renderer.OnRenderRibbonItemText(new RibbonTextEventArgs(Owner, e.Graphics, Bounds, this, TextBoxTextBounds, TextBoxText, f));
 
                if (LabelVisible)
                {
                    f.Alignment = (StringAlignment)TextAlignment;
                    Owner.Renderer.OnRenderRibbonItemText(new RibbonTextEventArgs(Owner, e.Graphics, Bounds, this, LabelBounds, Text, f));
                }
            }
        }
 
        public override void SetBounds(Rectangle bounds)
        {
            base.SetBounds(bounds);
 
            _textBoxBounds = Rectangle.FromLTRB(
                bounds.Right - TextBoxWidth - _UpDownSize,
                bounds.Top,
                bounds.Right - _UpDownSize,
                bounds.Bottom);
 
            if (Image != null)
                _imageBounds = new Rectangle(
                    bounds.Left + Owner.ItemMargin.Left,
                    bounds.Top + Owner.ItemMargin.Top, Image.Width, Image.Height);
            else
                _imageBounds = new Rectangle(bounds.Location, Size.Empty);
 
            _labelBounds = Rectangle.FromLTRB(
                _imageBounds.Right + (_imageBounds.Width > 0 ? spacing : 0),
                bounds.Top,
                _textBoxBounds.Left - spacing,
                bounds.Bottom - Owner.ItemMargin.Bottom);
 
            UpButtonBounds = new Rectangle(bounds.Right - _UpDownSize, bounds.Top, _UpDownSize, bounds.Height / 2);
            DownButtonBounds = new Rectangle(UpButtonBounds.X, UpButtonBounds.Bottom + 1, UpButtonBounds.Width, bounds.Height - UpButtonBounds.Height);
 
            if (SizeMode == RibbonElementSizeMode.Large)
            {
                _imageVisible = true;
                _labelVisible = true;
            }
            else if (SizeMode == RibbonElementSizeMode.Medium)
            {
                _imageVisible = true;
                _labelVisible = false;
                _labelBounds = Rectangle.Empty;
            }
            else if (SizeMode == RibbonElementSizeMode.Compact)
            {
                _imageBounds = Rectangle.Empty;
                _imageVisible = false;
                _labelBounds = Rectangle.Empty;
                _labelVisible = false;
            }
        }
 
        public override Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e)
        {
            if (!Visible && !Owner.IsDesignMode())
            {
                SetLastMeasuredSize(new Size(0, 0));
                return LastMeasuredSize;
            }
 
            Size size = Size.Empty;
 
            int w = 0;
            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 + spacing;
            int twidth = TextBoxWidth;
 
            w += TextBoxWidth + _UpDownSize;
 
            switch (e.SizeMode)
            {
                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);
            if (TextBoxBounds.Contains(e.Location))
                Canvas.Cursor = Cursors.IBeam;
        }
 
        public override void OnMouseLeave(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseLeave(e);
 
            UpButtonPressed = false;
            DownButtonPressed = false;
            UpButtonSelected = false;
            DownButtonSelected = false;
 
            Canvas.Cursor = Cursors.Default;
        }
 
        public override void OnMouseUp(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseUp(e);
            bool mustRedraw = false;
 
            if (UpButtonPressed || DownButtonPressed)
                mustRedraw = true;
 
            UpButtonPressed = false;
            DownButtonPressed = false;
 
            if (mustRedraw)
                RedrawItem();
        }
 
        public override void OnMouseDown(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            if (UpButtonBounds.Contains(e.Location))
            {
                UpButtonPressed = true;
                DownButtonPressed = false;
                DownButtonSelected = false;
                if (UpButtonClicked != null)
                    UpButtonClicked(this, e);
            }
            else if (DownButtonBounds.Contains(e.Location))
            {
                DownButtonPressed = true;
                UpButtonPressed = false;
                UpButtonSelected = false;
                if (DownButtonClicked != null)
                    DownButtonClicked(this, e);
            }
            else if (TextBoxBounds.Contains(e.X, e.Y) && AllowTextEdit)
            {
                StartEdit();
            }
        }
 
        public override void OnMouseMove(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseMove(e);
 
            bool mustRedraw = false;
 
            if (UpButtonBounds.Contains(e.Location))
            {
                Owner.Cursor = Cursors.Default;
                mustRedraw = !UpButtonSelected || DownButtonSelected || DownButtonPressed;
                UpButtonSelected = true;
                DownButtonSelected = false;
                DownButtonPressed = false;
            }
            else if (DownButtonBounds.Contains(e.Location))
            {
                Owner.Cursor = Cursors.Default;
                mustRedraw = !DownButtonSelected || UpButtonSelected || UpButtonPressed;
                DownButtonSelected = true;
                UpButtonSelected = false;
                UpButtonPressed = false;
            }
            else if (TextBoxBounds.Contains(e.X, e.Y))
            {
                Owner.Cursor = Cursors.IBeam;
                mustRedraw = DownButtonSelected || DownButtonPressed || UpButtonSelected || UpButtonPressed;
                UpButtonSelected = false;
                UpButtonPressed = false;
                DownButtonSelected = false;
                DownButtonPressed = false;
            }
            else
            {
                Owner.Cursor = Cursors.Default;
            }
 
            if (mustRedraw)
            {
                RedrawItem();
            }
        }
 
        #endregion
 
        #region Properties
 
        ///// <summary>
        ///// Gets the Ribbon this DropDown belongs to
        ///// </summary>
        //public Ribbon OwnerRibbon
        //{
        //   get { return _ownerRibbon; }
        //}
 
        /// <summary>
        /// Gets a value indicating if the Up button is currently pressed
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool UpButtonPressed { get; private set; }
 
        /// <summary>
        /// Gets a value indicating if the Down button is currently pressed
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool DownButtonPressed { get; private set; }
 
        /// <summary>
        /// Gets a value indicating if the Up button is currently selected
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool UpButtonSelected { get; private set; }
 
        /// <summary>
        /// Gets a value indicating if the Down button is currently selected
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool DownButtonSelected { get; private set; }
 
        /// <summary>
        /// Gets or sets the bounds of the DropDown button
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Rectangle UpButtonBounds { get; private set; }
 
        /// <summary>
        /// Gets or sets the bounds of the DropDown button
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Rectangle DownButtonBounds { get; private set; }
 
        ///// <summary>
        ///// Overriden.
        ///// </summary>
        //[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        //public override Rectangle TextBoxTextBounds
        //{
        //   get
        //   {
        //      Rectangle r = base.TextBoxTextBounds;
 
        //      r.Width -= _UpButtonBounds.Width;
 
        //      return r;
        //   }
        //}
        #endregion
    }
}