yangyin
2024-10-14 af90d73f1740a8e15fe2c76282fb7e2ccad5df5d
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// *********************************
// 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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
 
namespace System.Windows.Forms
{
    [Designer(typeof(RibbonComboBoxDesigner))]
    public class RibbonComboBox
        : RibbonTextBox, IContainsRibbonComponents, IDropDownRibbonItem
    {
        #region Fields
        // Steve
        private RibbonItem _selectedItem;
        private readonly Set<RibbonItem> _assignedHandlers = new Set<RibbonItem>();
        #endregion
 
        #region Events
 
        /// <summary>
        /// Raised when the DropDown is about to be displayed
        /// </summary>
        public event EventHandler DropDownShowing;
 
        public event RibbonItemEventHandler DropDownItemClicked;
        public delegate void RibbonItemEventHandler(object sender, RibbonItemEventArgs e);
 
        #endregion
 
        #region Ctor
 
        public RibbonComboBox()
        {
            DropDownItems = new RibbonItemCollection();
            DropDownItems.SetOwnerItem(this);
            DropDownVisible = false;
            AllowTextEdit = true;
            DrawIconsBar = true;
            DropDownMaxHeight = 0;
            _disableTextboxCursor = true;
        }
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                RemoveHandlers();
            }
            base.Dispose(disposing);
        }
 
        #endregion
 
        #region Properties
 
        /// <summary>
        /// Gets or sets the maximum height for the dropdown window.  0 = autosize.  If the size is smaller than the contents then scrollbars will be shown.
        /// </summary>
        [DefaultValue(0)]
        [Category("Behavior")]
        [Description("Gets or sets the maximum height for the dropdown window.  0 = Autosize.  If the size is smaller than the contents then scrollbars will be shown.")]
        public int DropDownMaxHeight { get; set; }
 
        /// <summary>
        /// Gets or sets a value indicating if the DropDown portion of the combo box is currently shown.
        /// </summary>
        [Browsable(false)]
        [DefaultValue(false)]
        [Description("Indicates if the dropdown window is currently visible")]
        public bool DropDownVisible { get; private set; }
 
        /// <summary>
        /// Gets or sets a value indicating if the DropDown should be resizable
        /// </summary>
        [DefaultValue(false)]
        [Category("Drop Down")]
        [Description("Makes the DropDown resizable with a grip on the corner")]
        public bool DropDownResizable { get; set; }
 
        /// <summary>
        /// Overriden.
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public override Rectangle TextBoxTextBounds
        {
            get
            {
                Rectangle r = base.TextBoxTextBounds;
 
                r.Width -= DropDownButtonBounds.Width;
 
                return r;
            }
        }
 
        /// <summary>
        /// Gets the collection of items to be displayed on the dropdown
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Category("Drop Down")]
        public RibbonItemCollection DropDownItems { get; }
 
        // Steve
        /// <summary>
        /// Gets the selected of item on the dropdown
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public RibbonItem SelectedItem
        {
            get
            {
                if (_selectedItem == null)
                {
                    foreach (RibbonItem item in DropDownItems)
                    {
                        if (item.Text == TextBoxText)
                        {
                            _selectedItem = item;
                            return item;
                        }
                    }
                    return null;
                }
 
                if (DropDownItems.Contains(_selectedItem))
                {
                    return _selectedItem;
                }
 
                _selectedItem = null;
                return null;
            }
            //Steve
            set
            {
                if (value == null)
                {
                    _selectedItem = null;
                    TextBoxText = String.Empty;
                }
                else if (value.GetType().BaseType == typeof(RibbonItem))
                {
                    if (DropDownItems.Contains(value))
                    {
                        _selectedItem = value;
                        TextBoxText = _selectedItem.Text;
                    }
                    else
                    {
                        //_dropDownItems.Add(value);
                        _selectedItem = value;
                        TextBoxText = _selectedItem.Text;
                    }
                }
            }
        }
 
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int SelectedIndex
        {
            get
            {
                if (_selectedItem != null)
                {
                    return DropDownItems.IndexOf(_selectedItem);
                }
                else
                {
                    return -1;
                }
            }
            set
            {
                if (value == -1)
                {
                    SelectedItem = null;
                }
                else
                {
                    if ((DropDownItems.Count > 0) &&
                        (value >= 0) &&
                        (value < DropDownItems.Count))
                    {
                        SelectedItem = DropDownItems[value];
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException(nameof(SelectedIndex));
                    }
                }
            }
        }
 
        // Kevin
        /// <summary>
        /// Gets or sets the value of selected item on the dropdown.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedValue
        {
            get
            {
                if (_selectedItem == null)
                {
                    return null;
                }
 
                return _selectedItem.Value;
            }
            set
            {
                foreach (RibbonItem item in DropDownItems)
                {
                    if (String.Compare(item.Value, value, false) == 0)
                    {
                        if (_selectedItem != item)
                        {
                            _selectedItem = item;
                            TextBoxText = _selectedItem.Text;
                            RibbonItemEventArgs arg = new RibbonItemEventArgs(item);
                            OnDropDownItemClicked(ref arg);
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// Gets the DropDown of the button
        /// </summary>
        internal RibbonDropDown DropDown { get; private set; }
 
        #endregion
 
        #region Methods
 
        /// <summary>
        /// Raises the <see cref="DropDownShowing"/> event;
        /// </summary>
        /// <param name="e"></param>
        public void OnDropDownShowing(EventArgs e)
        {
            if (DropDownShowing != null)
            {
                DropDownShowing(this, e);
            }
        }
 
        /// <summary>
        /// Creates the DropDown menu
        /// </summary>
        protected virtual void CreateDropDown()
        {
            DropDown = new RibbonDropDown(this, DropDownItems, Owner);
        }
 
        /// <summary>
        /// Gets or sets if the icons bar should be drawn
        /// </summary>
        [Category("Drop Down")]
        [DisplayName("DrawDropDownIconsBar")]
        [DefaultValue(true)]
        public bool DrawIconsBar { get; set; }
 
        /// <summary>
        /// Shows the DropDown
        /// </summary>
        public virtual void ShowDropDown()
        {
            if (!DropDownVisible)
            {
                AssignHandlers();
                OnDropDownShowing(EventArgs.Empty);
 
                //Highlight (select) the item on the drop down that matches the ComboBox text.
                foreach (RibbonItem item in DropDownItems)
                {
                    item.SetSelected(item == SelectedItem);
                }
 
                CreateDropDown();
 
                DropDown.DropDownMaxHeight = DropDownMaxHeight;
                DropDown.ShowSizingGrip = DropDownResizable;
                DropDown.DrawIconsBar = DrawIconsBar;
                DropDown.Closed += DropDown_Closed;
 
                Point location = OnGetDropDownMenuLocation();
                DropDown.Show(location);
                DropDownVisible = true;
            }
        }
 
        private void DropDown_Closed(object sender, EventArgs e)
        {
            DropDownVisible = false;
 
            //Steve - when popup closed, un-highlight the dropdown arrow and redraw
            DropDownButtonPressed = false;
            //Kevin - Unselect it as well
            DropDownButtonSelected = false;
 
            SetSelected(false);
 
            RedrawItem();
        }
 
        private void AssignHandlers()
        {
            foreach (RibbonItem item in DropDownItems)
            {
                if (_assignedHandlers.Contains(item) == false)
                {
                    item.Click += DropDownItem_Click;
                    _assignedHandlers.Add(item);
                }
            }
        }
 
        private void RemoveHandlers()
        {
            foreach (RibbonItem item in _assignedHandlers)
            {
                item.Click -= DropDownItem_Click;
            }
            _assignedHandlers.Clear();
        }
 
        private void DropDownItem_Click(object sender, EventArgs e)
        {
            // Steve
            _selectedItem = (sender as RibbonItem);
 
            TextBoxText = (sender as RibbonItem).Text;
            //Kevin Carbis
            RibbonItemEventArgs ev = new RibbonItemEventArgs(sender as RibbonItem);
            OnDropDownItemClicked(ref ev);
        }
 
 
        #endregion
 
        #region Overrides
 
        protected override bool ClosesDropDownAt(Point p)
        {
            return false;
        }
 
        protected override void InitTextBox(TextBox t)
        {
            base.InitTextBox(t);
 
            t.Width -= DropDownButtonBounds.Width;
        }
 
        public override void SetBounds(Rectangle bounds)
        {
            base.SetBounds(bounds);
 
            DropDownButtonBounds = Rectangle.FromLTRB(
                bounds.Right - 15,
                bounds.Top,
                bounds.Right + 1,
                bounds.Bottom + 1);
        }
 
        public virtual void OnDropDownItemClicked(ref RibbonItemEventArgs e)
        {
            if (DropDownItemClicked != null)
            {
                DropDownItemClicked(this, e);
            }
        }
 
        public override void OnMouseMove(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseMove(e);
 
            bool mustRedraw = false;
 
            if (DropDownButtonBounds.Contains(e.X, e.Y))
            {
                Owner.Cursor = Cursors.Default;
 
                mustRedraw = !DropDownButtonSelected;
 
                DropDownButtonSelected = true;
            }
            else if (TextBoxBounds.Contains(e.X, e.Y))
            {
                Owner.Cursor = AllowTextEdit ? Cursors.IBeam : Cursors.Default;
 
                mustRedraw = DropDownButtonSelected;
 
                DropDownButtonSelected = false;
            }
            else
            {
                Owner.Cursor = Cursors.Default;
            }
 
            if (mustRedraw)
            {
                RedrawItem();
            }
        }
 
        public override void OnMouseDown(MouseEventArgs e)
        {
            if (!Enabled) return;
            // Steve - if allowtextedit is false, allow the textbox to bring up the popup
            if (DropDownButtonBounds.Contains(e.X, e.Y) || (TextBoxBounds.Contains(e.X, e.Y) != AllowTextEdit))
            {
                DropDownButtonPressed = true;
 
                ShowDropDown();
            }
            else if (TextBoxBounds.Contains(e.X, e.Y) && AllowTextEdit)
            {
                StartEdit();
            }
        }
 
        public override void OnMouseUp(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseUp(e);
 
            //Steve - pressed if set false when popup is closed
            //_dropDownPressed = false;
        }
 
        public override void OnMouseLeave(MouseEventArgs e)
        {
            if (!Enabled) return;
 
            base.OnMouseLeave(e);
 
            DropDownButtonSelected = false;
 
        }
 
        /// <summary>
        /// Gets the location where the dropdown menu will be shown
        /// </summary>
        /// <returns></returns>
        internal virtual Point OnGetDropDownMenuLocation()
        {
            Point location = Point.Empty;
 
            if (Canvas is RibbonDropDown)
            {
                location = Canvas.PointToScreen(new Point(TextBoxBounds.Left, Bounds.Bottom));
            }
            else
            {
                location = Owner.PointToScreen(new Point(TextBoxBounds.Left, Bounds.Bottom));
            }
 
            return location;
        }
 
        #endregion
 
        #region IContainsRibbonComponents Members
 
        public IEnumerable<Component> GetAllChildComponents()
        {
            return DropDownItems.ToArray();
        }
 
        #endregion
 
        #region IDropDownRibbonItem Members
 
        /// <summary>
        /// Gets or sets the bounds of the DropDown button
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Rectangle DropDownButtonBounds { get; private set; }
 
        /// <summary>
        /// Gets a value indicating if the DropDown is currently visible
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool DropDownButtonVisible => DropDownVisible;
 
        /// <summary>
        /// Gets a value indicating if the DropDown button is currently selected
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool DropDownButtonSelected { get; private set; }
 
        /// <summary>
        /// Gets a value indicating if the DropDown button is currently pressed
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool DropDownButtonPressed { get; private set; }
 
        internal override void SetOwner(Ribbon owner)
        {
            base.SetOwner(owner);
 
            DropDownItems.SetOwner(owner);
        }
 
        internal override void SetOwnerPanel(RibbonPanel ownerPanel)
        {
            base.SetOwnerPanel(ownerPanel);
 
            DropDownItems.SetOwnerPanel(ownerPanel);
        }
 
        internal override void SetOwnerTab(RibbonTab ownerTab)
        {
            base.SetOwnerTab(ownerTab);
 
            DropDownItems.SetOwnerTab(OwnerTab);
        }
 
        internal override void SetOwnerItem(RibbonItem ownerItem)
        {
            base.SetOwnerItem(ownerItem);
        }
 
        internal override void ClearOwner()
        {
            List<RibbonItem> oldItems = new List<RibbonItem>(DropDownItems);
 
            base.ClearOwner();
 
            foreach (RibbonItem item in oldItems)
            {
                item.ClearOwner();
            }
        }
 
        #endregion
    }
}