-
Notifications
You must be signed in to change notification settings - Fork 4
/
MdiTabStrip.cs
709 lines (604 loc) · 18.5 KB
/
MdiTabStrip.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
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
namespace Menees.Windows.Forms
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion
/// <summary>
/// Displays an <see cref="MdiTab"/> for each MDI child form.
/// </summary>
public sealed partial class MdiTabStrip : ExtendedToolStrip
{
#region Private Data Members
private static readonly IList<Form> NoForms = CollectionUtility.EmptyArray<Form>();
private int updateLevel;
private MdiTab? dragTab;
#endregion
#region Constructors
/// <summary>
/// Creates a new instance.
/// </summary>
public MdiTabStrip()
{
this.InitializeComponent();
this.AllowDrop = true;
this.GripStyle = ToolStripGripStyle.Hidden;
this.UseFormIconAsNewTabImage = true;
MdiTabStripColorTable colorTable = new(this);
ToolStripProfessionalRenderer renderer = new(colorTable);
this.Renderer = renderer;
// Call this to make it initially invisible (if it's not design time).
this.UpdateTabStrip(null);
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets a value indicating whether drag-and-drop item reordering is allowed.
/// </summary>
/// <remarks>
/// This defaults to true so tab items can be reordered by the user via drag-and-drop.
/// It is hidden at a design-time because it shouldn't be changed for MdiTabStrips.
/// </remarks>
[DefaultValue(true)]
[Browsable(false)]
public override bool AllowDrop
{
get
{
return base.AllowDrop;
}
set
{
if (value)
{
this.AllowItemReorder = false;
}
base.AllowDrop = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether tabs can be reordered when ALT is pressed.
/// </summary>
/// <remarks>
/// This defaults to false because its drag-and-drop is incompatible with our standard
/// drag-and-drop (because AllowItemReorder will let you drag items to other ToolStrips).
/// It is hidden at a design-time because it shouldn't be changed for MdiTabStrips.
/// </remarks>
[DefaultValue(false)]
[Browsable(false)]
public new bool AllowItemReorder
{
get { return base.AllowItemReorder; }
set { base.AllowItemReorder = value; }
}
/// <summary>
/// Gets or sets whether the move handle is visible or hidden.
/// </summary>
/// <remarks>
/// This defaults to Hidden, which is opposite of how it defaults in the base ToolStrip.
/// </remarks>
[DefaultValue(ToolStripGripStyle.Hidden)]
public new ToolStripGripStyle GripStyle
{
get { return base.GripStyle; }
set { base.GripStyle = value; }
}
/// <summary>
/// Gets or sets how the tab strip is rendered.
/// </summary>
/// <remarks>
/// This class always uses a custom renderer, so the RenderMode property
/// should not be changed.
/// </remarks>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new ToolStripRenderMode RenderMode
{
get { return base.RenderMode; }
set { base.RenderMode = value; }
}
/// <summary>
/// Gets or sets whether the image for a new tab should be extracted from the form's icon.
/// </summary>
/// <remarks>
/// This defaults to true. Set it to false if you want to manually set each tab's image.
/// </remarks>
[DefaultValue(true)]
[Description("Whether the image for a new tab should be extracted from the form's icon.")]
public bool UseFormIconAsNewTabImage
{
get;
set;
}
/// <summary>
/// Gets the current collection of tabs.
/// </summary>
[Browsable(false)]
public IEnumerable<MdiTab> Tabs
{
get
{
IEnumerable<MdiTab> result = this.GetTabs();
return result;
}
}
#endregion
#region Private Properties
private Form? ActiveMdiChild
{
get
{
Form? result = null;
Form? ownerForm = this.FindForm();
if (ownerForm != null)
{
result = ownerForm.ActiveMdiChild;
}
return result;
}
}
private IList<Form> MdiChildren
{
get
{
IList<Form> result;
Form ownerForm = this.FindForm();
if (ownerForm != null)
{
result = ownerForm.MdiChildren;
}
else
{
result = NoForms;
}
return result;
}
}
#endregion
#region Public Methods
/// <summary>
/// Prevents the tab strip from updating until <see cref="EndUpdate"/> is called.
/// </summary>
/// <remarks>
/// This is useful when adding or removing many forms at once. You should use
/// a try..finally block to make sure EndUpdate gets called.
/// </remarks>
public void BeginUpdate()
{
this.updateLevel++;
}
/// <summary>
/// Resumes updating the tab strip after being suspended by the <see cref="BeginUpdate"/> method.
/// </summary>
public void EndUpdate()
{
this.updateLevel--;
if (this.updateLevel == 0)
{
// Pass false for "senderActivating", so UpdateTabStrip
// will determine the correct new active tab.
this.UpdateTabStrip(null, false);
}
}
/// <summary>
/// Closes the tab (and form) for the active MDI child form.
/// </summary>
public void CloseActiveTab()
{
Form? child = this.ActiveMdiChild;
if (child != null)
{
child.Close();
}
}
/// <summary>
/// Closes every tab (and form) except the one for the active MDI child form.
/// </summary>
public void CloseAllButActiveTab()
{
Form? child = this.ActiveMdiChild;
if (child != null)
{
this.BeginUpdate();
try
{
foreach (MdiTab tab in this.GetTabs())
{
Form? form = tab.AssociatedForm;
if (form != null && form != child)
{
form.Close();
}
}
}
finally
{
this.EndUpdate();
}
}
}
/// <summary>
/// Finds the tab for the active MDI child form.
/// </summary>
/// <returns>The active tab, or null if there is no active child form.</returns>
public MdiTab? FindActiveTab()
{
Form? child = this.ActiveMdiChild;
MdiTab? result = this.FindTab(child);
return result;
}
/// <summary>
/// Finds the tab for the specified MDI child form.
/// </summary>
/// <param name="form">The form to get the tab for.</param>
/// <returns>The tab for the specified form, or null if no tab exists for the form
/// (e.g., if the form is closed).</returns>
public MdiTab? FindTab(Form? form)
{
MdiTab? result = this.FindTab(form, null);
return result;
}
/// <summary>
/// Gets the tab at the specified screen point (e.g., <see cref="Cursor.Position"/>).
/// </summary>
/// <param name="point">The screen point to look under.</param>
/// <returns>The tab under the specified point or null.</returns>
/// <remarks>
/// This method is useful for preventing the opening of a context menu strip
/// unless the mouse cursor is currently over a tab.
/// </remarks>
public MdiTab? FindTabAtScreenPoint(Point point)
{
Point clientPoint = this.PointToClient(point);
MdiTab? result = this.FindDisplayedTabAtClientPoint(clientPoint, true);
return result;
}
#endregion
#region Internal Methods
internal void BeginDragTimer(MdiTab tab)
{
if (!this.dragTimer.Enabled)
{
this.dragTab = tab;
this.Capture = true;
// This is slow enough that most normal, quick left clicks won't begin a drag
// but fast enough that pressing and holding to begin a drag doesn't make
// you wait an awkward amount of time.
const int BeginDragMilliseconds = 200;
// The drag timer ensures that normal, quick left clicks won't begin a drag,
// which would change the cursor to a Move cursor. The default seems like a
// good response time to me, but if the mouse hover time is quicker, then we'll
// use that. Setting this on each timer usage allows us to react to system
// setting changes. It also prevents us from getting a CA1601 code analysis
// warning about mobility power management concerns with fast timers:
// http://msdn.microsoft.com/en-us/library/ms182230.aspx
this.dragTimer.Interval = Math.Min(BeginDragMilliseconds, SystemInformation.MouseHoverTime);
this.dragTimer.Enabled = true;
}
}
internal void EndDragTimer(bool allowDrag)
{
this.dragTimer.Enabled = false;
bool hadCapture = this.Capture;
this.Capture = false;
if (allowDrag)
{
// Make sure the click is still held on the drag tab and there is more than one tab visible.
if (hadCapture && Control.MouseButtons == MouseButtons.Left && this.dragTab != null &&
this.dragTab == this.FindDisplayedTabAtClientPoint(this.PointToClient(Control.MousePosition), true) &&
this.GetDisplayedTabs().Count > 1)
{
this.dragTab.DoDragDrop(this.dragTab, DragDropEffects.Move);
}
}
this.dragTab = null;
}
#endregion
#region Protected Methods
/// <summary>
/// Overrides <see cref="Control.OnParentChanged"/>.
/// </summary>
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
Form form = this.FindForm();
if (form != null)
{
form.MdiChildActivate += this.Form_MdiChildActivate;
}
}
/// <summary>
/// Overrides <see cref="Control.OnEnabledChanged"/>.
/// </summary>
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
// Use an update batch, so the control will fully refresh when re-enabled.
this.BeginUpdate();
this.EndUpdate();
}
#endregion
#region Private Helper Methods
private static bool IsAvailable([NotNullWhen(true)] Form? form)
{
bool result = form != null && !form.Disposing && !form.IsDisposed && form.Visible && form.IsMdiChild;
return result;
}
private MdiTab? FindTab(Form? form, IList<MdiTab>? tabs)
{
MdiTab? result = null;
if (form != null)
{
if (tabs == null)
{
tabs = this.GetTabs();
}
result = tabs.FirstOrDefault(tab => tab.AssociatedForm == form);
}
return result;
}
private IList<MdiTab> GetTabs()
{
// Call ToList() to create a snapshot because callers may need to modify the this.Items collection.
// Restrict to MdiTabs in case a caller has added other controls to the tool strip (e.g., a right-aligned,
// never overflowing Close button).
var result = this.Items.OfType<MdiTab>().ToList();
return result;
}
private IList<MdiTab> GetDisplayedTabs()
{
// See comments in GetTabs about why ToList is used.
var result = this.DisplayedItems.OfType<MdiTab>().ToList();
return result;
}
private void UpdateTabStrip(MdiTab? sender, bool senderActivating = true)
{
// Don't do anything if we're in the middle of a Begin/EndUpdate batch.
if (this.updateLevel == 0 && this.Enabled)
{
// Remove tabs for closed forms.
HashSet<Form> skipForms = new();
IList<MdiTab> tabs = this.RemoveClosedTabs(sender, senderActivating, skipForms);
// Add tabs for new forms.
tabs = this.AddNewTabs(tabs, skipForms);
// If the old sender was deactivating, then we need to activate the tab for the new active window.
// Also, if a user right-clicks directly on a tab's close button, it will close the tab's form and then
// we'll get the tab click event. So we need to detect when a sender thinks it's activating even
// though its form has already closed. (Update: Now I've made right-click not fire Close though.)
MdiTab? activeTab = sender;
if (!senderActivating || activeTab == null || activeTab.AssociatedForm == null)
{
activeTab = this.FindTab(this.ActiveMdiChild, tabs);
}
// Update the active tab.
foreach (MdiTab tab in tabs)
{
Form? form = tab.AssociatedForm;
if (tab == activeTab)
{
tab.Checked = true;
if (IsAvailable(form))
{
form.Select();
}
}
else
{
tab.Checked = false;
}
tab.Text = form?.Text;
}
// Make sure the active tab is visible.
if (activeTab != null && activeTab.IsOnOverflow)
{
this.Items.Insert(0, activeTab);
}
// Show or hide the tab strip as necessary.
if (!this.DesignMode)
{
this.Visible = tabs.Count > 0;
}
}
}
private IList<MdiTab> RemoveClosedTabs(MdiTab? sender, bool senderActivating, HashSet<Form> skipForms)
{
IList<MdiTab> result = this.GetTabs();
bool removedTabs = false;
foreach (MdiTab tab in result)
{
Form? form = tab.AssociatedForm;
if ((!senderActivating && sender == tab) || !IsAvailable(form))
{
if (form != null)
{
skipForms.Add(form);
form.FormClosed -= this.ChildForm_FormClosed;
form.TextChanged -= this.ChildForm_TextChanged;
}
this.Items.Remove(tab);
tab.AssociatedForm = null;
removedTabs = true;
// This case shouldn't ever occur, but we might as well be safe.
if (this.dragTab == tab)
{
this.EndDragTimer(false);
}
}
}
// Update the tab list if any were removed.
if (removedTabs)
{
result = this.GetTabs();
}
return result;
}
private IList<MdiTab> AddNewTabs(IList<MdiTab> tabs, HashSet<Form> skipForms)
{
// RemoveClosedTabs should have gotten rid of any tabs with null AssociatedForms,
// but filtering them out again just to be sure is safe and quick.
Dictionary<Form, MdiTab> formToTabMap = tabs.Where(t => t.AssociatedForm != null).ToDictionary(tab => tab.AssociatedForm!);
bool addedTabs = false;
foreach (Form form in this.MdiChildren)
{
if (IsAvailable(form) && !skipForms.Contains(form) && !formToTabMap.TryGetValue(form, out MdiTab? tab))
{
tab = new MdiTab(form);
tab.Click += this.Tab_Click;
tab.CloseClicked += this.Tab_CloseClicked;
if (this.UseFormIconAsNewTabImage && form.Icon != null)
{
tab.Image = WindowsUtility.GetSmallIconImage(form.Icon);
// Note: I originally set tab.ImageTransparentColor = Color.Transparent here,
// but that made some white areas in the icons appear transparent even though
// they shouldn't have been. That seems like a .NET bug since icons already
// have explicitly transparent areas.
}
form.FormClosed += this.ChildForm_FormClosed;
form.TextChanged += this.ChildForm_TextChanged;
// Add it to the beginning like VS does. This keeps new tabs from immediately going
// into the overflow area when lots of forms are open. This is also consistent with
// how activating a tab from the overflow area moves it to the beginning of the tab list.
this.Items.Insert(0, tab);
addedTabs = true;
}
}
// Update the tab list if any were added.
IList<MdiTab> result = addedTabs ? this.GetTabs() : tabs;
return result;
}
private void HandleDrag(DragEventArgs e, bool finished)
{
if (e.Data != null && e.Data.GetData(typeof(ToolStripItem)) is MdiTab tab)
{
var displayedTabs = this.GetDisplayedTabs();
Point clientPoint = this.PointToClient(new Point(e.X, e.Y));
MdiTab? targetTab = this.FindDisplayedTabAtClientPoint(clientPoint, false);
if (targetTab != null)
{
int targetIndex = displayedTabs.IndexOf(targetTab);
if (targetIndex >= 0)
{
// Determine the target index based on if we're on the left or right side of the button.
Rectangle targetBounds = targetTab.Bounds;
if (clientPoint.X > (targetBounds.Left + (targetBounds.Width / 2)))
{
targetIndex++;
}
int originalIndex = displayedTabs.IndexOf(tab);
if (originalIndex != targetIndex)
{
// Decrement by 1 if new index is > current index because the Insert
// is going to remove the current tab before re-inserting it.
if (targetIndex > originalIndex)
{
targetIndex--;
}
this.Items.Insert(targetIndex, tab);
}
}
}
// Make sure the dragged tab is selected/highlighted
// to make the drag object clear to the user.
tab.Select();
// If we're done dragging, make sure the tab is checked/activated correctly.
if (finished)
{
this.UpdateTabStrip(tab, true);
}
}
}
private MdiTab? FindDisplayedTabAtClientPoint(Point clientPoint, bool exact)
{
MdiTab? result = null;
foreach (MdiTab tab in this.GetDisplayedTabs())
{
Rectangle bounds = tab.Bounds;
// If the point is before the first button, then pretend it's in the first button.
if (bounds.Contains(clientPoint) || (!exact && clientPoint.X < bounds.X && this.Bounds.Contains(clientPoint)))
{
result = tab;
break;
}
}
return result;
}
#endregion
#region Private Event Handlers
private void Tab_Click(object? sender, EventArgs e)
{
this.UpdateTabStrip(sender as MdiTab);
}
[SuppressMessage("Design", "CC0091:Use static method", Justification = "WinForms designer expects instance level.")]
private void Tab_CloseClicked(object? sender, EventArgs e)
{
if (sender is MdiTab tab)
{
Form? form = tab.AssociatedForm;
if (form != null)
{
form.Close();
}
}
}
private void ChildForm_FormClosed(object? sender, FormClosedEventArgs e)
{
MdiTab? tab = this.FindTab(sender as Form);
this.UpdateTabStrip(tab, false);
}
private void ChildForm_TextChanged(object? sender, EventArgs e)
{
if (sender is Form form)
{
MdiTab? tab = this.FindTab(form);
if (tab != null)
{
tab.Text = form.Text;
// Note: There's no need to call UpdateUI because changing
// the form's text only changes the tab caption. It doesn't
// show, hide, activate, or do anything else to it.
}
}
}
private void Form_MdiChildActivate(object? sender, EventArgs e)
{
// This method is needed to detect newly added child forms.
// When a new MDI child is first shown, this event fires, and
// our UpdateTabStrip call will add a tab for it.
MdiTab? tab = this.FindTab(this.ActiveMdiChild);
this.UpdateTabStrip(tab);
}
[SuppressMessage("Design", "CC0091:Use static method", Justification = "WinForms designer expects instance level.")]
private void MdiTabStrip_DragEnter(object? sender, DragEventArgs e)
{
// Don't allow ToolStripItems from other ToolStrips to be dragged into this ToolStrip.
if (e.Data != null && e.Data.GetDataPresent(typeof(ToolStripItem)) && e.Data.GetData(typeof(ToolStripItem)) is MdiTab)
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void MdiTabStrip_DragOver(object? sender, DragEventArgs e)
{
this.HandleDrag(e, false);
}
private void MdiTabStrip_DragDrop(object? sender, DragEventArgs e)
{
this.HandleDrag(e, true);
}
private void DragTimer_Tick(object? sender, EventArgs e)
{
this.EndDragTimer(true);
}
#endregion
}
}