tangxu
2024-10-22 6a07c4c846ffbb1e93afdf0260e123e4c145f419
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
// *********************************
// 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
{
    /// <summary>
    /// Represents a tab that can contain RibbonPanel objects
    /// </summary>
    [DesignTimeVisible(false)]
    [Designer(typeof(RibbonTabDesigner))]
    public class RibbonTab : Component, IRibbonElement, IRibbonToolTip, IContainsRibbonComponents
    {
        #region Fields
        private bool? _isopeninvisualstudiodesigner;
        private bool _enabled;
        private bool _pressed;
        private bool _selected;
        private bool _active;
        private string _text;
        private RibbonContext _context;
        private int _offset;
        private bool _visible = true;
 
        private readonly RibbonToolTip _TT;
 
        /// <summary>
        /// Occurs when the mouse pointer enters the panel
        /// </summary>
        public event MouseEventHandler MouseEnter;
 
        /// <summary>
        /// Occurs when the mouse pointer leaves the panel
        /// </summary>
        public event MouseEventHandler MouseLeave;
 
        /// <summary>
        /// Occurs when the mouse pointer is moved inside the panel
        /// </summary>
        public event MouseEventHandler MouseMove;
 
        #endregion
 
        #region Ctor
 
        public RibbonTab()
        {
            Panels = new RibbonPanelCollection(this);
            _enabled = true;
 
            //Initialize the ToolTip for this Item
            _TT = new RibbonToolTip(this)
            {
                InitialDelay = 100,
                AutomaticDelay = 800,
                AutoPopDelay = 8000,
                UseAnimation = true,
                Active = false
            };
            _TT.Popup += _TT_Popup;
        }
 
        public RibbonTab(string text)
           : this()
        {
            _text = text;
        }
 
        /// <summary>
        /// Creates a new RibbonTab
        /// </summary>
        [Obsolete("Use 'public RibbonTab(string text)' instead!")]
        public RibbonTab(Ribbon owner, string text)
           : this(text)
        {
        }
 
        /// <summary>
        /// RibbonTab is open in Visual Studio Designer
        /// </summary>
        protected bool IsOpenInVisualStudioDesigner()
        {
            if (!_isopeninvisualstudiodesigner.HasValue)
            {
                _isopeninvisualstudiodesigner = LicenseManager.UsageMode == LicenseUsageMode.Designtime ||
                                                this.DesignMode;
                if (!_isopeninvisualstudiodesigner.Value)
                {
                    try
                    {
                        using (var process = System.Diagnostics.Process.GetCurrentProcess())
                        {
                            _isopeninvisualstudiodesigner = process.ProcessName.ToLowerInvariant().Contains("devenv");
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return _isopeninvisualstudiodesigner.Value;
        }
 
        /// <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 && RibbonDesigner.Current == null)
            {
                // ADDED
                _TT.Popup -= _TT_Popup;
 
                _TT.Dispose();
                try
                {
                    foreach (RibbonPanel p in Panels)
                        p.Dispose();
                }
                catch (InvalidOperationException)
                {
                    if (!IsOpenInVisualStudioDesigner())
                    {
                        throw;
                    }
                }
            }
 
            base.Dispose(disposing);
        }
 
        #endregion
 
        #region Events
 
 
        public event EventHandler ScrollRightVisibleChanged;
        public event EventHandler ScrollRightPressedChanged;
        public event EventHandler ScrollRightBoundsChanged;
        public event EventHandler ScrollRightSelectedChanged;
        public event EventHandler ScrollLeftVisibleChanged;
        public event EventHandler ScrollLeftPressedChanged;
        public event EventHandler ScrollLeftSelectedChanged;
        public event EventHandler ScrollLeftBoundsChanged;
        public event EventHandler TabBoundsChanged;
        public event EventHandler TabContentBoundsChanged;
        public event EventHandler OwnerChanged;
        public event EventHandler PressedChanged;
        public event EventHandler ActiveChanged;
        public event EventHandler TextChanged;
        public event EventHandler ContextChanged;
        public virtual event RibbonElementPopupEventHandler ToolTipPopUp;
 
        #endregion
 
        #region Props
 
        private string _Name = string.Empty;
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public virtual string Name
        {
            get
            {
                if (Site != null)
                {
                    _Name = Site.Name;
                }
                return _Name;
            }
            set => _Name = value;
        }
 
        [DefaultValue(true)]
        [Category("Behavior")]
        [Description("Sets if the tab should be enabled")]
        public bool Enabled
        {
            get
            {
                if (Owner != null)
                {
                    return _enabled && Owner.Enabled;
                }
 
                return _enabled;
            }
            set
            {
                _enabled = value;
                Owner.Invalidate();
 
                foreach (RibbonPanel item in Panels)
                {
                    item.Enabled = value;
                }
            }
        }
 
        /// <summary>
        /// Gets if the right-side scroll button is currently visible
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public bool ScrollRightVisible { get; private set; }
 
        /// <summary>
        /// Gets if the right-side scroll button is currently selected
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public bool ScrollRightSelected { get; private set; }
 
        /// <summary>
        /// Gets if the right-side scroll button is currently pressed
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public bool ScrollRightPressed { get; private set; }
 
        /// <summary>
        /// Gets if the right-side scroll button bounds
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Rectangle ScrollRightBounds { get; private set; }
 
        /// <summary>
        /// Gets if the left scroll button is currently visible
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Browsable(false)]
        public bool ScrollLeftVisible { get; private set; }
 
        /// <summary>
        /// Gets if the left scroll button bounds
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Rectangle ScrollLeftBounds { get; private set; }
 
        /// <summary>
        /// Gets if the left scroll button is currently selected
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public bool ScrollLeftSelected { get; private set; }
 
        /// <summary>
        /// Gets if the left scroll button is currently pressed
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public bool ScrollLeftPressed { get; private set; }
 
        /// <summary>
        /// Gets the <see cref="TabBounds"/> property value
        /// </summary>
        [Browsable(false)]
        public Rectangle Bounds => TabBounds;
 
        /// <summary>
        /// Gets the collection of panels that belong to this tab
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RibbonPanelCollection Panels { get; }
 
        /// <summary>
        /// Gets the bounds of the little tab showing the text
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Rectangle TabBounds { get; private set; }
 
        /// <summary>
        /// Gets the bounds of the tab content on the Ribbon
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Rectangle TabContentBounds { get; private set; }
 
        /// <summary>
        /// Gets the Ribbon that contains this tab
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Ribbon Owner { get; private set; }
 
        /// <summary>
        /// Gets a value indicating whether the state of the tab is being pressed by the mouse or a key
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual bool Pressed => _pressed;
 
        /// <summary>
        /// Gets a value indicating whether the tab is selected
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual bool Selected => _selected;
 
        /// <summary>
        /// Gets a value indicating if the tab is currently the active tab
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual bool Active => _active;
 
        /// <summary>
        /// Gets or sets the object that contains data about the control
        /// </summary>
        [Description("An Object field for associating custom data for this control")]
        [DefaultValue(null)]
        [Category("Data")]
        [TypeConverter(typeof(StringConverter))]
        public object Tag { get; set; }
 
        /// <summary>
        /// Gets or sets the custom string data associated with this control
        /// </summary>
        [DefaultValue(null)]
        [Category("Data")]
        [Description("A string field for associating custom data for this control")]
        public string Value { get; set; }
 
        /// <summary>
        /// Gets or sets the key combination that activates this element when the Alt key was pressed
        /// </summary>
        [Category("Behavior")]
        [DefaultValue(null)]
        public string AltKey { get; set; }
 
 
        /// <summary>
        /// Gets or sets the text that is to be displayed on the tab
        /// </summary>
        [Localizable(true)]
        [Category("Appearance")]
        public string Text
        {
            get => _text;
            set
            {
 
                _text = value;
 
                OnTextChanged(EventArgs.Empty);
 
                if (Owner != null) Owner.OnRegionsChanged();
            }
        }
 
        /// <summary>
        /// Gets a value indicating whether the tab is attached to a  Context
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual bool Contextual => _context != null;
 
        /// <summary>
        /// Gets or sets the context this tab belongs to
        /// </summary>
        /// <remarks>Tabs on a context are highlighted with a special glow color</remarks>
        [DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public RibbonContext Context
        {
            get => _context;
            set
            {
                _context = value;
 
                OnContextChanged(EventArgs.Empty);
 
                if (Owner != null)
                {
                    Owner.OnRegionsChanged();
                    Owner.UpdateRegions();
                }
            }
        }
 
        /// <summary>
        /// Gets or sets the visibility of this tab
        /// </summary>
        /// <remarks>Tabs on a context are highlighted with a special glow color</remarks>
        [Category("Behavior")]
        [Localizable(true)]
        [DefaultValue(true)]
        public bool Visible
        {
            get
            {
                if (Owner != null && !Owner.IsDesignMode() && !Owner.Visible)
                    return false;
 
                //If this tab has a context, follow the visibility of the context.
                return (Contextual) ? Context.Visible : _visible;
            }
            set
            {
                _visible = value;
                if (Owner != null)
                {
                    Owner.UpdateRegions();
                    if (Active)
                    {
                        EnsureAnyTabVisible();
                    }
                    else
                    {
                        Owner.Invalidate();
                    }
                }
            }
        }
 
        /// <summary>
        /// Gets or sets the tool tip title
        /// </summary>
        [DefaultValue("")]
        public string ToolTipTitle
        {
            get => _TT.ToolTipTitle;
            set => _TT.ToolTipTitle = value;
        }
 
        /// <summary>
        /// Gets or sets the image of the tool tip
        /// </summary>
        [DefaultValue(ToolTipIcon.None)]
        public ToolTipIcon ToolTipIcon
        {
            get => _TT.ToolTipIcon;
            set => _TT.ToolTipIcon = value;
        }
 
        /// <summary>
        /// Gets or sets the tool tip text
        /// </summary>
        [DefaultValue(null)]
        [Localizable(true)]
        public string ToolTip { get; set; }
 
        /// <summary>
        /// Gets or sets the tool tip image
        /// </summary>
        [DefaultValue(null)]
        [Localizable(true)]
        public Image ToolTipImage
        {
            get => _TT.ToolTipImage;
            set => _TT.ToolTipImage = value;
        }
 
        /// <summary>
        /// Gets whether the tab should be drawn invisible.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal bool Invisible => Owner != null && Owner.HideSingleTabIfTextEmpty && Owner.Tabs.Count == 1 && string.IsNullOrEmpty(Text);
 
        #endregion
 
        #region IRibbonElement Members
 
        public void OnPaint(object sender, RibbonElementPaintEventArgs e)
        {
            if (Owner == null) return;
 
            Owner.Renderer.OnRenderRibbonTab(new RibbonTabRenderEventArgs(Owner, e.Graphics, e.Clip, this));
            Owner.Renderer.OnRenderRibbonTabText(new RibbonTabRenderEventArgs(Owner, e.Graphics, e.Clip, this));
 
            if (Active && (!Owner.Minimized || (Owner.Minimized && Owner.Expanded)))
            {
                int displayIndex = 0;
                foreach (RibbonPanel panel in Panels)
                {
                    if (panel.Visible)
                    {
                        panel.Index = displayIndex;
                        panel.IsFirstPanel = (displayIndex == 0);
 
                        panel.OnPaint(this, new RibbonElementPaintEventArgs(e.Clip, e.Graphics, panel.SizeMode, e.Control));
 
                        displayIndex++;
                    }
                }
 
                foreach (RibbonPanel panel in Panels)
                {
                    if (panel.Visible)
                    {
                        panel.IsLastPanel = (panel.Index == displayIndex - 1);
                        break;
                    }
                }
            }
 
            Owner.Renderer.OnRenderTabScrollButtons(new RibbonTabRenderEventArgs(Owner, e.Graphics, e.Clip, this));
        }
 
        /// <summary>
        /// This method is not relevant for this class
        /// </summary>
        /// <exception cref="NotSupportedException">Always</exception>
        public void SetBounds(Rectangle bounds)
        {
            throw new NotSupportedException();
        }
 
        /// <summary>
        /// Gets or sets the context this tab belongs to
        /// </summary>
        /// <remarks>Tabs on a context are highlighted with a special glow color</remarks>
        /// <param name="context"></param>
        public void SetContext(RibbonContext context)
        {
            bool trigger = !context.Equals(context);
 
            if (trigger)
                OnContextChanged(EventArgs.Empty);
 
            _context = context;
        }
 
        /// <summary>
        /// Measures the size of the tab. The tab content bounds is measured by the Ribbon control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e)
        {
            if (!Visible && !Owner.IsDesignMode()) return new Size(0, 0);
 
            return Size.Ceiling(e.Graphics.MeasureString(string.IsNullOrEmpty(Text) ? " " : Text, Owner.Font));
        }
 
        /// <summary>
        /// Sets the value of the Owner Property
        /// </summary>
        internal void SetOwner(Ribbon owner)
        {
            Owner = owner;
 
            Panels.SetOwner(owner);
 
            OnOwnerChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// When an item is removed from the RibbonItemCollection remove all its references.
        /// </summary>
        internal virtual void ClearOwner()
        {
            Owner = null;
            OnOwnerChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Sets the value of the Pressed property
        /// </summary>
        /// <param name="pressed">Value that indicates if the element is pressed</param>
        internal void SetPressed(bool pressed)
        {
            _pressed = pressed;
 
            OnPressedChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Sets the value of the Selected property
        /// </summary>
        /// <param name="selected">Value that indicates if the element is selected</param>
        internal void SetSelected(bool selected)
        {
            _selected = selected;
 
            if (selected)
            {
                OnMouseEnter(new MouseEventArgs(MouseButtons.None, 0, 0, 0, 0));
            }
            else
            {
                OnMouseLeave(new MouseEventArgs(MouseButtons.None, 0, 0, 0, 0));
            }
        }
 
        #endregion
 
        #region Method Triggers
 
        public void OnContextChanged(EventArgs e)
        {
            if (ContextChanged != null)
            {
                ContextChanged(this, e);
            }
        }
 
        public void OnTextChanged(EventArgs e)
        {
            if (TextChanged != null)
            {
                TextChanged(this, e);
            }
        }
 
        public void OnActiveChanged(EventArgs e)
        {
            if (ActiveChanged != null)
            {
                ActiveChanged(this, e);
            }
        }
 
        public void OnPressedChanged(EventArgs e)
        {
            if (PressedChanged != null)
            {
                PressedChanged(this, e);
            }
        }
 
        public void OnOwnerChanged(EventArgs e)
        {
            if (OwnerChanged != null)
            {
                OwnerChanged(this, e);
            }
        }
 
        public void OnTabContentBoundsChanged(EventArgs e)
        {
            if (TabContentBoundsChanged != null)
            {
                TabContentBoundsChanged(this, e);
            }
        }
 
        public void OnTabBoundsChanged(EventArgs e)
        {
            if (TabBoundsChanged != null)
            {
                TabBoundsChanged(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="ScrollRightVisibleChanged"/> event
        /// </summary>
        /// <param name="e">Event data</param>
        public void OnScrollRightVisibleChanged(EventArgs e)
        {
            if (ScrollRightVisibleChanged != null)
            {
                ScrollRightVisibleChanged(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="ScrollRightPressedChanged"/> event
        /// </summary>
        /// <param name="e">Event data</param>
        public void OnScrollRightPressedChanged(EventArgs e)
        {
            if (ScrollRightPressedChanged != null)
            {
                ScrollRightPressedChanged(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="ScrollRightBoundsChanged"/> event
        /// </summary>
        /// <param name="e">Event data</param>
        public void OnScrollRightBoundsChanged(EventArgs e)
        {
            if (ScrollRightBoundsChanged != null)
            {
                ScrollRightBoundsChanged(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="ScrollRightSelectedChanged"/> event
        /// </summary>
        /// <param name="e">Event data</param>
        public void OnScrollRightSelectedChanged(EventArgs e)
        {
            if (ScrollRightSelectedChanged != null)
            {
                ScrollRightSelectedChanged(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="ScrollLeftVisibleChanged"/> event
        /// </summary>
        /// <param name="e">Event data</param>
        public void OnScrollLeftVisibleChanged(EventArgs e)
        {
            if (ScrollLeftVisibleChanged != null)
            {
                ScrollLeftVisibleChanged(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="ScrollLeftPressedChanged"/> event
        /// </summary>
        /// <param name="e">Event data</param>
        public void OnScrollLeftPressedChanged(EventArgs e)
        {
            if (ScrollLeftPressedChanged != null)
            {
                ScrollLeftPressedChanged(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="ScrollLeftBoundsChanged"/> event
        /// </summary>
        /// <param name="e">Event data</param>
        public void OnScrollLeftBoundsChanged(EventArgs e)
        {
            if (ScrollLeftBoundsChanged != null)
            {
                ScrollLeftBoundsChanged(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="ScrollLeftSelectedChanged"/> event
        /// </summary>
        /// <param name="e">Event data</param>
        public void OnScrollLeftSelectedChanged(EventArgs e)
        {
            if (ScrollLeftSelectedChanged != null)
            {
                ScrollLeftSelectedChanged(this, e);
            }
        }
 
        #endregion
 
        #region Methods
        /// <summary>
        /// Sets the tab as active without sending the message to the Ribbon
        /// </summary>
        internal void SetActive(bool active)
        {
            bool trigger = _active != active;
 
            _active = active;
 
            if (trigger)
                OnActiveChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Sets the value of the TabBounds property
        /// </summary>
        /// <param name="tabBounds">Rectangle representing the bounds of the tab</param>
        internal void SetTabBounds(Rectangle tabBounds)
        {
            bool tigger = TabBounds != tabBounds;
 
            TabBounds = tabBounds;
 
            OnTabBoundsChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Sets the value of the TabContentBounds
        /// </summary>
        /// <param name="tabContentBounds">Rectangle representing the bounds of the tab's content</param>
        internal void SetTabContentBounds(Rectangle tabContentBounds)
        {
            bool trigger = TabContentBounds != tabContentBounds;
 
            TabContentBounds = tabContentBounds;
 
            OnTabContentBoundsChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Gets the panel with the larger width and the specified size mode
        /// </summary>
        /// <param name="size">Size mode of panel to search</param>
        /// <returns>Larger panel. Null if none of the specified size mode</returns>
        private RibbonPanel GetLargerPanel(RibbonElementSizeMode size)
        {
            RibbonPanel result = null;
 
            foreach (RibbonPanel panel in Panels)
            {
                if (panel.SizeMode != size) continue;
 
                if (result == null) result = panel;
 
                if (panel.Bounds.Width > result.Bounds.Width)
                {
                    result = panel;
                }
            }
 
            return result;
        }
 
        /// <summary>
        /// Gets the panel with a larger size
        /// </summary>
        /// <returns></returns>
        private RibbonPanel GetLargerPanel()
        {
            RibbonPanel largeLarger = GetLargerPanel(RibbonElementSizeMode.Large);
 
            if (largeLarger != null) return largeLarger;
 
            RibbonPanel mediumLarger = GetLargerPanel(RibbonElementSizeMode.Medium);
 
            if (mediumLarger != null) return mediumLarger;
 
            RibbonPanel compactLarger = GetLargerPanel(RibbonElementSizeMode.Compact);
 
            if (compactLarger != null) return compactLarger;
 
            RibbonPanel overflowLarger = GetLargerPanel(RibbonElementSizeMode.Overflow);
 
            if (overflowLarger != null) return overflowLarger;
 
            return null;
        }
 
        private bool AllPanelsOverflow()
        {
 
            foreach (RibbonPanel panel in Panels)
            {
                if (panel.SizeMode != RibbonElementSizeMode.Overflow)
                {
                    return false;
                }
            }
 
            return true;
        }
 
        /// <summary>
        /// Updates the regions of the panels and its contents
        /// </summary>
        internal void UpdatePanelsRegions()
        {
            if (Panels.Count == 0) return;
            if (Owner == null || Owner.IsDisposed) return;
 
            if (!Owner.IsDesignMode())
                _offset = 0;
 
            int curRight = TabContentBounds.Left + Owner.PanelPadding.Left + _offset;
            int curLeft = TabContentBounds.Right - Owner.PanelPadding.Right;
            int panelsTop = TabContentBounds.Top + Owner.PanelPadding.Top;
            int panelsVisibles = 0;
 
            using (Graphics g = Owner.CreateGraphics())
            {
                //Check all at full size
                foreach (RibbonPanel panel in Panels)
                {
                    if (panel.Visible && Owner.RightToLeft == RightToLeft.No)
                    {
                        RibbonElementSizeMode sMode = panel.FlowsTo == RibbonPanelFlowDirection.Right ? RibbonElementSizeMode.Medium : RibbonElementSizeMode.Large;
                        //Set the bounds of the panel to let it know it's height
                        panel.SetBounds(new Rectangle(0, 0, 1, TabContentBounds.Height - Owner.PanelPadding.Vertical));
 
                        //Size of the panel
                        Size size = panel.MeasureSize(this, new RibbonElementMeasureSizeEventArgs(g, sMode));
 
                        //Creates the bounds of the panel
                        Rectangle bounds = new Rectangle(
                             curRight, panelsTop,
                             size.Width, size.Height);
 
                        //Set the bounds of the panel
                        panel.SetBounds(bounds);
 
                        //Let the panel know what size we have decided for it
                        panel.SetSizeMode(sMode);
 
                        //Update curRight
                        curRight = bounds.Right + Owner.PanelSpacing;
 
                        //Update panelsVisibles
                        panelsVisibles += 1;
                    }
                    else if (panel.Visible && Owner.RightToLeft == RightToLeft.Yes)
                    {
                        RibbonElementSizeMode sMode = panel.FlowsTo == RibbonPanelFlowDirection.Right ? RibbonElementSizeMode.Medium : RibbonElementSizeMode.Large;
 
                        //Set the bounds of the panel to let it know it's height
                        panel.SetBounds(new Rectangle(0, 0, 1, TabContentBounds.Height - Owner.PanelPadding.Vertical));
 
                        //Size of the panel
                        Size size = panel.MeasureSize(this, new RibbonElementMeasureSizeEventArgs(g, sMode));
 
                        curLeft -= size.Width + Owner.PanelSpacing;
 
                        //Creates the bounds of the panel
                        Rectangle bounds = new Rectangle(
                             curLeft, panelsTop,
                             size.Width, size.Height);
 
                        //Set the bounds of the panel
                        panel.SetBounds(bounds);
 
                        //Let the panel know what size we have decided for it
                        panel.SetSizeMode(sMode);
 
                        //Update curLeft
                        curLeft = bounds.Left - 1 - Owner.PanelSpacing;
 
                        //Update panelsVisibles
                        panelsVisibles += 1;
                    }
                    else
                    {
                        panel.SetBounds(Rectangle.Empty);
                    }
                }
 
                if (!Owner.IsDesignMode())
                {
                    //check if there are visible panels
                    if (panelsVisibles > 0)
                    {
                        while (curRight > TabContentBounds.Right && !AllPanelsOverflow())
                        {
                            #region Down grade the larger panel one position
 
                            RibbonPanel larger = GetLargerPanel();
 
                            if (larger.SizeMode == RibbonElementSizeMode.Large)
                                larger.SetSizeMode(RibbonElementSizeMode.Medium);
                            else if (larger.SizeMode == RibbonElementSizeMode.Medium)
                                larger.SetSizeMode(RibbonElementSizeMode.Compact);
                            else if (larger.SizeMode == RibbonElementSizeMode.Compact)
                                larger.SetSizeMode(RibbonElementSizeMode.Overflow);
 
                            Size size = larger.MeasureSize(this, new RibbonElementMeasureSizeEventArgs(g, larger.SizeMode));
 
                            larger.SetBounds(new Rectangle(larger.Bounds.Location, new Size(size.Width + Owner.PanelMargin.Horizontal, size.Height)));
 
                            #endregion
 
                            //Reset x-axis reminder
                            curRight = TabContentBounds.Left + Owner.PanelPadding.Left;
 
                            //Re-arrange location because of the new bounds
                            foreach (RibbonPanel panel in Panels)
                            {
                                Size s = panel.Bounds.Size;
                                panel.SetBounds(new Rectangle(new Point(curRight, panelsTop), s));
                                curRight += panel.Bounds.Width + Owner.PanelSpacing;
                            }
 
                        }
                    }
                }
 
                //Update regions of all panels
                foreach (RibbonPanel panel in Panels)
                {
                    panel.UpdateItemsRegions(g, panel.SizeMode);
                }
            }
 
            UpdateScrollBounds();
        }
 
        /// <summary>
        /// Updates the bounds of the scroll bounds
        /// </summary>
        private void UpdateScrollBounds()
        {
            int w = 13;
            bool scrBuffer = ScrollRightVisible;
            bool sclBuffer = ScrollLeftVisible;
            Rectangle rrBuffer = ScrollRightBounds;
            Rectangle rlBuffer = ScrollLeftBounds;
 
            if (Panels.Count == 0) return;
 
 
 
            if (Panels[Panels.Count - 1].Bounds.Right > TabContentBounds.Right)
            {
                ScrollRightVisible = true;
            }
            else
            {
                ScrollRightVisible = false;
            }
 
            if (ScrollRightVisible != scrBuffer)
            {
                OnScrollRightVisibleChanged(EventArgs.Empty);
            }
 
 
 
            if (_offset < 0)
            {
                ScrollLeftVisible = true;
            }
            else
            {
                ScrollLeftVisible = false;
            }
 
            if (ScrollRightVisible != scrBuffer)
            {
                OnScrollLeftVisibleChanged(EventArgs.Empty);
            }
 
            if (ScrollLeftVisible || ScrollRightVisible)
            {
                ScrollRightBounds = Rectangle.FromLTRB(
                     Owner.ClientRectangle.Right - w,
                     TabContentBounds.Top,
                     Owner.ClientRectangle.Right,
                     TabContentBounds.Bottom);
 
                ScrollLeftBounds = Rectangle.FromLTRB(
                     0,
                     TabContentBounds.Top,
                     w,
                     TabContentBounds.Bottom);
 
                if (ScrollRightBounds != rrBuffer)
                {
                    OnScrollRightBoundsChanged(EventArgs.Empty);
                }
 
                if (ScrollLeftBounds != rlBuffer)
                {
                    OnScrollLeftBoundsChanged(EventArgs.Empty);
                }
            }
        }
 
        /// <summary>
        /// Overriden. Returns a string representation of the tab
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("Tab: {0}", Text);
        }
 
        /// <summary>
        /// Raises the MouseEnter event
        /// </summary>
        /// <param name="e">Event data</param>
        public virtual void OnMouseEnter(MouseEventArgs e)
        {
            if (MouseEnter != null)
            {
                MouseEnter(this, e);
            }
        }
 
        /// <summary>
        /// Raises the MouseLeave event
        /// </summary>
        /// <param name="e">Event data</param>
        public virtual void OnMouseLeave(MouseEventArgs e)
        {
            _TT.Active = false;
 
            if (MouseLeave != null)
            {
                MouseLeave(this, e);
            }
        }
 
        /// <summary>
        /// Raises the MouseMove event
        /// </summary>
        /// <param name="e">Event data</param>
        public virtual void OnMouseMove(MouseEventArgs e)
        {
            if (MouseMove != null)
            {
                MouseMove(this, e);
            }
            if (!_TT.Active && !string.IsNullOrEmpty(ToolTip))  // ToolTip should be working without title as well - to get Office 2007 Look & Feel
            {
                if (ToolTip != _TT.GetToolTip(Owner))
                {
                    _TT.SetToolTip(Owner, ToolTip);
                }
                _TT.Active = true;
            }
        }
 
        /// <summary>
        /// Sets the value of the <see cref="ScrollLeftPressed"/>
        /// </summary>
        /// <param name="pressed"></param>
        internal void SetScrollLeftPressed(bool pressed)
        {
            ScrollLeftPressed = pressed;
 
            if (pressed)
                ScrollLeft();
 
            OnScrollLeftPressedChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Sets the value of the <see cref="ScrollLeftSelected"/>
        /// </summary>
        /// <param name="selected"></param>
        internal void SetScrollLeftSelected(bool selected)
        {
            ScrollLeftSelected = selected;
 
            OnScrollLeftSelectedChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Sets the value of the <see cref="ScrollRightPressed"/>
        /// </summary>
        /// <param name="pressed"></param>
        internal void SetScrollRightPressed(bool pressed)
        {
            ScrollRightPressed = pressed;
 
            if (pressed) ScrollRight();
 
            OnScrollRightPressedChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Sets the value of the <see cref="ScrollRightSelected"/>
        /// </summary>
        /// <param name="selected"></param>
        internal void SetScrollRightSelected(bool selected)
        {
            ScrollRightSelected = selected;
 
            OnScrollRightSelectedChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Presses the lef-scroll button
        /// </summary>
        public void ScrollLeft()
        {
            ScrollOffset(50);
        }
 
        /// <summary>
        /// Presses the left-scroll button
        /// </summary>
        public void ScrollRight()
        {
            ScrollOffset(-50);
        }
 
        public void ScrollOffset(int amount)
        {
            _offset += amount;
 
            foreach (RibbonPanel p in Panels)
            {
                p.SetBounds(new Rectangle(p.Bounds.Left + amount,
                     p.Bounds.Top, p.Bounds.Width, p.Bounds.Height));
            }
 
            if (Site != null && Site.DesignMode)
                UpdatePanelsRegions();
 
            UpdateScrollBounds();
 
            Owner.Invalidate();
        }
 
        private void _TT_Popup(object sender, PopupEventArgs e)
        {
            if (ToolTipPopUp != null)
            {
                ToolTipPopUp(sender, new RibbonElementPopupEventArgs(this, e));
                if (ToolTip != _TT.GetToolTip(Owner))
                    _TT.SetToolTip(Owner, ToolTip);
            }
        }
 
        private void EnsureAnyTabVisible()
        {
            int myIndex = Owner.Tabs.IndexOf(this);
            // check if any of the next tabs is visible
            for (int i = myIndex; i < Owner.Tabs.Count; i++)
            {
                if (this == Owner.Tabs[i])
                    continue;
                // check member, Property is false if control is hidden
                if (Owner.Tabs[i]._visible)
                {
                    Owner.ActiveTab = Owner.Tabs[i];
                    return;  // return because Invalidate is done during setting the active tab
                }
            }
            // check if any of the previous tabs is visible
            for (int i = 0; i < myIndex; i++)
            {
                if (this == Owner.Tabs[i])
                    continue;
                // check member, Property is false if control is hidden
                if (Owner.Tabs[i]._visible)
                {
                    Owner.ActiveTab = Owner.Tabs[i];
                    return;  // return because Invalidate is done during setting the active tab
                }
            }
            Owner.Invalidate();
        }
 
        #endregion
 
        #region IContainsRibbonComponents Members
 
        public IEnumerable<Component> GetAllChildComponents()
        {
            return Panels.ToArray();
        }
 
        #endregion
    }
}