-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix rectangle of the ListViewGroup #4800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
1 commit merged into
dotnet:main
from
SergeySmirnov-Akvelon:Issue-4778_ListViewGroup_invalid_bounds
Apr 21, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.LVGGR.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class ComCtl32 | ||
| { | ||
| public enum LVGGR | ||
| { | ||
| GROUP = 0, | ||
| HEADER = 1, | ||
| LABEL = 2, | ||
| SUBSETLINK = 3 | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3501,22 +3501,6 @@ internal int GetColumnIndex(ColumnHeader ch) | |
| return -1; | ||
| } | ||
|
|
||
| internal int GetGroupIndex(ListViewGroup owningGroup) | ||
|
||
| { | ||
| if (Groups.Count == 0) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| // Default group it's last group in accessibility and not specified in Groups collection, therefore default group index = Groups.Count | ||
| if (DefaultGroup == owningGroup) | ||
| { | ||
| return Groups.Count; | ||
| } | ||
|
|
||
| return Groups.IndexOf(owningGroup); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the current ListViewItem corresponding to the specific | ||
| /// x,y co-ordinate. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| using System.Collections.Generic; | ||
| using System.Drawing; | ||
| using System.Runtime.InteropServices; | ||
| using static System.Windows.Forms.ListView; | ||
| using static Interop; | ||
| using static Interop.ComCtl32; | ||
|
|
@@ -42,28 +43,55 @@ public override Rectangle Bounds | |
| { | ||
| get | ||
| { | ||
| if (!_owningListView.IsHandleCreated || _owningListView.VirtualMode) | ||
| if (!_owningListView.IsHandleCreated || !_owningListViewAccessibilityObject.ShowGroupAccessibleObject) | ||
| { | ||
| return Rectangle.Empty; | ||
| } | ||
|
|
||
| RECT groupRect = new RECT(); | ||
| User32.SendMessageW(_owningListView, (User32.WM)ComCtl32.LVM.GETGROUPRECT, (IntPtr)CurrentIndex, ref groupRect); | ||
| if (GetVisibleItems().Count == 0) | ||
| { | ||
| return Rectangle.Empty; | ||
| } | ||
|
|
||
| int nativeGroupId = GetNativeGroupId(); | ||
| if (nativeGroupId == -1) | ||
| { | ||
| return Rectangle.Empty; | ||
| } | ||
|
|
||
| return new Rectangle( | ||
| _owningListViewAccessibilityObject.Bounds.X + groupRect.left, | ||
| _owningListViewAccessibilityObject.Bounds.Y + groupRect.top, | ||
| groupRect.right - groupRect.left, | ||
| groupRect.bottom - groupRect.top); | ||
| LVGGR rectType = _owningGroup.CollapsedState == ListViewGroupCollapsedState.Collapsed | ||
| ? LVGGR.HEADER | ||
| : LVGGR.GROUP; | ||
|
|
||
| // Get the native rectangle | ||
| RECT groupRect = new(); | ||
|
|
||
| // Using the "top" property, we set which rectangle type of the group we want to get | ||
| // This is described in more detail in https://docs.microsoft.com/windows/win32/controls/lvm-getgrouprect | ||
|
||
| groupRect.top = (int)rectType; | ||
| User32.SendMessageW(_owningListView, (User32.WM)LVM.GETGROUPRECT, (IntPtr)nativeGroupId, ref groupRect); | ||
|
|
||
| // Using the following code, we limit the size of the ListViewGroup rectangle | ||
| // so that it does not go beyond the rectangle of the ListView | ||
| Rectangle listViewBounds = _owningListView.AccessibilityObject.Bounds; | ||
SergeySmirnov-Akvelon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| groupRect = _owningListView.RectangleToScreen(groupRect); | ||
| groupRect.top = Math.Max(listViewBounds.Top, groupRect.top); | ||
| groupRect.bottom = Math.Min(listViewBounds.Bottom, groupRect.bottom); | ||
| groupRect.left = Math.Max(listViewBounds.Left, groupRect.left); | ||
| groupRect.right = Math.Min(listViewBounds.Right, groupRect.right); | ||
|
|
||
| return groupRect; | ||
| } | ||
| } | ||
|
|
||
| private int CurrentIndex | ||
| internal int CurrentIndex | ||
| // The default group has 0 index, as it is always displayed first. | ||
| => _owningGroupIsDefault | ||
| // Default group has the last index out of the Groups.Count | ||
| // upper bound: so the DefaultGroup.Index == Groups.Count. | ||
| ? _owningListView.Groups.Count | ||
SergeySmirnov-Akvelon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| : _owningListView.Groups.IndexOf(_owningGroup); | ||
| ? 0 | ||
| // When calculating the index of other groups, we add a shift if the default group is displayed | ||
| : _owningListViewAccessibilityObject.OwnerHasDefaultGroup | ||
| ? _owningListView.Groups.IndexOf(_owningGroup) + 1 | ||
| : _owningListView.Groups.IndexOf(_owningGroup); | ||
|
|
||
| public override string DefaultAction | ||
| => SR.AccessibleActionDoubleClick; | ||
|
|
@@ -73,6 +101,8 @@ internal override UiaCore.ExpandCollapseState ExpandCollapseState | |
| ? UiaCore.ExpandCollapseState.Collapsed | ||
| : UiaCore.ExpandCollapseState.Expanded; | ||
|
|
||
| internal override UiaCore.IRawElementProviderFragmentRoot FragmentRoot => _owningListView.AccessibilityObject; | ||
|
|
||
| public override string Name | ||
| => _owningGroup.Header; | ||
|
|
||
|
|
@@ -131,7 +161,26 @@ private bool GetNativeFocus() | |
| return false; | ||
| } | ||
|
|
||
| return LVGS.FOCUSED == unchecked((LVGS)(long)User32.SendMessageW(_owningListView, (User32.WM)LVM.GETGROUPSTATE, (IntPtr)CurrentIndex, (IntPtr)LVGS.FOCUSED)); | ||
| int nativeGroupId = GetNativeGroupId(); | ||
| if (nativeGroupId == -1) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return LVGS.FOCUSED == unchecked((LVGS)(long)User32.SendMessageW(_owningListView, (User32.WM)LVM.GETGROUPSTATE, (IntPtr)nativeGroupId, (IntPtr)LVGS.FOCUSED)); | ||
| } | ||
|
|
||
| private unsafe int GetNativeGroupId() | ||
| { | ||
| LVGROUPW lvgroup = new LVGROUPW | ||
| { | ||
| cbSize = (uint)sizeof(LVGROUPW), | ||
| mask = LVGF.GROUPID, | ||
| }; | ||
|
|
||
| return User32.SendMessageW(_owningListView, (User32.WM)LVM.GETGROUPINFOBYINDEX, (IntPtr)CurrentIndex, ref lvgroup) == IntPtr.Zero | ||
| ? -1 | ||
| : lvgroup.iGroupId; | ||
| } | ||
|
|
||
| internal override object? GetPropertyValue(UiaCore.UIA propertyID) | ||
|
|
@@ -157,6 +206,19 @@ private bool GetNativeFocus() | |
| internal IReadOnlyList<ListViewItem> GetVisibleItems() | ||
| { | ||
| List<ListViewItem> visibleItems = new(); | ||
| if (_owningGroupIsDefault) | ||
| { | ||
| foreach (ListViewItem? listViewItem in _owningListView.Items) | ||
| { | ||
| if (listViewItem is not null && listViewItem.Group is null) | ||
| { | ||
| visibleItems.Add(listViewItem); | ||
| } | ||
| } | ||
|
|
||
| return visibleItems; | ||
| } | ||
SergeySmirnov-Akvelon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| foreach (ListViewItem listViewItem in _owningGroup.Items) | ||
| { | ||
| if (listViewItem.ListView is not null) | ||
|
|
@@ -213,26 +275,13 @@ internal IReadOnlyList<ListViewItem> GetVisibleItems() | |
| return null; | ||
| } | ||
|
|
||
| if (!_owningGroupIsDefault) | ||
| IReadOnlyList<ListViewItem> visibleItems = GetVisibleItems(); | ||
| if (index < 0 || index >= visibleItems.Count) | ||
| { | ||
| IReadOnlyList<ListViewItem> visibleItems = GetVisibleItems(); | ||
| if (index < 0 || index >= visibleItems.Count) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return visibleItems[index].AccessibilityObject; | ||
| } | ||
|
|
||
| foreach (ListViewItem? item in _owningListView.Items) | ||
| { | ||
| if (item is not null && item.Group is null && index-- == 0) | ||
| { | ||
| return item.AccessibilityObject; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| return null; | ||
| return visibleItems[index].AccessibilityObject; | ||
| } | ||
|
|
||
| private int GetChildIndex(AccessibleObject child) | ||
|
|
@@ -300,23 +349,7 @@ public override int GetChildCount() | |
| return -1; | ||
| } | ||
|
|
||
| if (_owningGroupIsDefault) | ||
| { | ||
| int count = 0; | ||
| foreach (ListViewItem? item in _owningListView.Items) | ||
| { | ||
| if (item is not null && item.Group is null) | ||
| { | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
| else | ||
| { | ||
| return GetVisibleItems().Count; | ||
| } | ||
| return GetVisibleItems().Count; | ||
| } | ||
|
|
||
| internal override bool IsPatternSupported(UiaCore.UIA patternId) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.