Skip to content
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

Add ListBox/ComboBox tests and cleanup ListBox.SendMessage overloads #2703

Merged
merged 3 commits into from Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/System.Windows.Forms/src/System/Windows/Forms/ComboBox.cs
Expand Up @@ -1984,7 +1984,7 @@ private void ChildWndProc(ref Message m)
// Forward context menu messages to the parent control
if (ContextMenuStrip != null)
{
UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), WindowMessages.WM_CONTEXTMENU, m.WParam, m.LParam);
SendMessageW(this, WindowMessage.WM_CONTEXTMENU, m.WParam, m.LParam);
}
else
{
Expand Down Expand Up @@ -3584,7 +3584,7 @@ private void UpdateText()
{
if (childEdit != null && childEdit.Handle != IntPtr.Zero)
{
UnsafeNativeMethods.SendMessage(new HandleRef(this, childEdit.Handle), WindowMessages.WM_SETTEXT, IntPtr.Zero, s);
SendMessageW(new HandleRef(this, childEdit.Handle), WindowMessage.WM_SETTEXT, IntPtr.Zero, s);
}
}
}
Expand Down
122 changes: 67 additions & 55 deletions src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs
Expand Up @@ -300,7 +300,7 @@ public int ColumnWidth
}
else if (IsHandleCreated)
{
SendMessage((int)LB.SETCOLUMNWIDTH, columnWidth, 0);
SendMessageW(this, (WindowMessage)LB.SETCOLUMNWIDTH, (IntPtr)columnWidth);
}
}
}
Expand Down Expand Up @@ -469,7 +469,7 @@ internal int FocusedIndex
{
if (IsHandleCreated)
{
return unchecked((int)(long)SendMessage((int)LB.GETCARETINDEX, 0, 0));
return unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETCARETINDEX));
}

return -1;
Expand Down Expand Up @@ -667,7 +667,7 @@ public virtual int ItemHeight
if (drawMode == DrawMode.OwnerDrawFixed && IsHandleCreated)
{
BeginUpdate();
SendMessage((int)LB.SETITEMHEIGHT, 0, value);
SendMessageW(this, (WindowMessage)LB.SETITEMHEIGHT, IntPtr.Zero, (IntPtr)value);

// Changing the item height might require a resize for IntegralHeight list boxes
//
Expand Down Expand Up @@ -913,7 +913,7 @@ public override int SelectedIndex

if (current == SelectionMode.One && IsHandleCreated)
{
return unchecked((int)(long)SendMessage((int)LB.GETCURSEL, 0, 0));
return unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETCURSEL));
}

if (itemsCollection != null && SelectedItems.Count > 0)
Expand Down Expand Up @@ -1216,7 +1216,7 @@ public int TopIndex
{
if (IsHandleCreated)
{
return unchecked((int)(long)SendMessage((int)LB.GETTOPINDEX, 0, 0));
return unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETTOPINDEX));
}
else
{
Expand All @@ -1227,7 +1227,7 @@ public int TopIndex
{
if (IsHandleCreated)
{
SendMessage((int)LB.SETTOPINDEX, value, 0);
SendMessageW(this, (WindowMessage)LB.SETTOPINDEX, (IntPtr)value);
}
else
{
Expand Down Expand Up @@ -1531,7 +1531,7 @@ public int GetItemHeight(int index)

if (IsHandleCreated)
{
int h = unchecked((int)(long)SendMessage((int)LB.GETITEMHEIGHT, index, 0));
int h = unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETITEMHEIGHT, (IntPtr)index));
if (h == -1)
{
throw new Win32Exception();
Expand Down Expand Up @@ -1587,7 +1587,7 @@ private bool GetSelectedInternal(int index)
{
if (IsHandleCreated)
{
int sel = unchecked((int)(long)SendMessage((int)LB.GETSEL, index, 0));
int sel = unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETSEL, (IntPtr)index));
if (sel == -1)
{
throw new Win32Exception();
Expand Down Expand Up @@ -1624,7 +1624,7 @@ public int IndexFromPoint(int x, int y)
GetClientRect(new HandleRef(this, Handle), ref r);
if (r.left <= x && x < r.right && r.top <= y && y < r.bottom)
{
int index = unchecked((int)(long)SendMessage((int)LB.ITEMFROMPOINT, 0, unchecked((int)(long)PARAM.FromLowHigh(x, y))));
int index = unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.ITEMFROMPOINT, IntPtr.Zero, PARAM.FromLowHigh(x, y)));
if (PARAM.HIWORD(index) == 0)
{
// Inside ListBox client area
Expand All @@ -1642,8 +1642,7 @@ public int IndexFromPoint(int x, int y)
private int NativeAdd(object item)
{
Debug.Assert(IsHandleCreated, "Shouldn't be calling Native methods before the handle is created.");
int insertIndex = unchecked((int)(long)SendMessage((int)LB.ADDSTRING, 0, GetItemText(item)));

int insertIndex = unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.ADDSTRING, IntPtr.Zero, GetItemText(item)));
if (insertIndex == LB_ERRSPACE)
{
throw new OutOfMemoryException();
Expand All @@ -1667,15 +1666,15 @@ private int NativeAdd(object item)
private void NativeClear()
{
Debug.Assert(IsHandleCreated, "Shouldn't be calling Native methods before the handle is created.");
SendMessage((int)LB.RESETCONTENT, 0, 0);
SendMessageW(this, (WindowMessage)LB.RESETCONTENT);
}

/// <summary>
/// Get the text stored by the native control for the specified list item.
/// </summary>
internal string NativeGetItemText(int index)
{
int len = unchecked((int)(long)SendMessage((int)LB.GETTEXTLEN, index, 0));
int len = unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETTEXTLEN, (IntPtr)index));
StringBuilder sb = new StringBuilder(len + 1);
UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), (int)LB.GETTEXT, index, sb);
return sb.ToString();
Expand All @@ -1688,7 +1687,7 @@ internal string NativeGetItemText(int index)
private int NativeInsert(int index, object item)
{
Debug.Assert(IsHandleCreated, "Shouldn't be calling Native methods before the handle is created.");
int insertIndex = unchecked((int)(long)SendMessage((int)LB.INSERTSTRING, index, GetItemText(item)));
int insertIndex = unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.INSERTSTRING, (IntPtr)index, GetItemText(item)));

if (insertIndex == LB_ERRSPACE)
{
Expand All @@ -1715,8 +1714,8 @@ private void NativeRemoveAt(int index)
{
Debug.Assert(IsHandleCreated, "Shouldn't be calling Native methods before the handle is created.");

bool selected = (unchecked((int)(long)SendMessage((int)LB.GETSEL, (IntPtr)index, IntPtr.Zero)) > 0);
SendMessage((int)LB.DELETESTRING, index, 0);
bool selected = (unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETSEL, (IntPtr)index, IntPtr.Zero)) > 0);
SendMessageW(this, (WindowMessage)LB.DELETESTRING, (IntPtr)index);

//If the item currently selected is removed then we should fire a Selectionchanged event...
//as the next time selected index returns -1...
Expand All @@ -1738,11 +1737,11 @@ private void NativeSetSelected(int index, bool value)

if (selectionMode == SelectionMode.One)
{
SendMessage((int)LB.SETCURSEL, (value ? index : -1), 0);
SendMessageW(this, (WindowMessage)LB.SETCURSEL, (IntPtr)(value ? index : -1));
}
else
{
SendMessage((int)LB.SETSEL, value ? -1 : 0, index);
SendMessageW(this, (WindowMessage)LB.SETSEL, PARAM.FromBool(value), (IntPtr)index);
}
}

Expand All @@ -1751,7 +1750,7 @@ private void NativeSetSelected(int index, bool value)
/// query on that collection after we have called Dirty(). Dirty() is called
/// when we receive a LBN_SELCHANGE message.
/// </summary>
private void NativeUpdateSelection()
private unsafe void NativeUpdateSelection()
{
Debug.Assert(IsHandleCreated, "Should only call native methods if handle is created");

Expand All @@ -1768,7 +1767,7 @@ private void NativeUpdateSelection()
switch (selectionMode)
{
case SelectionMode.One:
int index = unchecked((int)(long)SendMessage((int)LB.GETCURSEL, 0, 0));
int index = unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETCURSEL));
if (index >= 0)
{
result = new int[] { index };
Expand All @@ -1778,11 +1777,14 @@ private void NativeUpdateSelection()

case SelectionMode.MultiSimple:
case SelectionMode.MultiExtended:
int count = unchecked((int)(long)SendMessage((int)LB.GETSELCOUNT, 0, 0));
int count = unchecked((int)(long)SendMessageW(this, (WindowMessage)LB.GETSELCOUNT));
if (count > 0)
{
result = new int[count];
UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), (int)LB.GETSELITEMS, count, result);
fixed (int* pResult = result)
{
SendMessageW(this, (WindowMessage)LB.GETSELITEMS, (IntPtr)count, (IntPtr)pResult);
}
}
break;
}
Expand Down Expand Up @@ -1814,17 +1816,20 @@ protected override void OnChangeUICues(UICuesEventArgs e)

protected override void OnGotFocus(EventArgs e)
{
AccessibleObject item = AccessibilityObject.GetFocused();

if (item != null)
{
HasKeyboardFocus = false;
item.RaiseAutomationEvent(UiaCore.UIA.AutomationFocusChangedEventId);
}
else
if (IsHandleCreated)
{
HasKeyboardFocus = true;
AccessibilityObject.RaiseAutomationEvent(UiaCore.UIA.AutomationFocusChangedEventId);
AccessibleObject item = AccessibilityObject.GetFocused();

if (item != null)
{
HasKeyboardFocus = false;
item.RaiseAutomationEvent(UiaCore.UIA.AutomationFocusChangedEventId);
}
else
{
HasKeyboardFocus = true;
AccessibilityObject.RaiseAutomationEvent(UiaCore.UIA.AutomationFocusChangedEventId);
}
}

base.OnGotFocus(e);
Expand All @@ -1847,34 +1852,38 @@ protected virtual void OnDrawItem(DrawItemEventArgs e)
/// set up a few things, like column width, etc! Inheriting classes should
/// not forget to call base.OnHandleCreated().
/// </summary>
protected override void OnHandleCreated(EventArgs e)
protected unsafe override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);

//for getting the current Locale to set the Scrollbars...
//
SendMessage((int)LB.SETLOCALE, CultureInfo.CurrentCulture.LCID, 0);
SendMessageW(this, (WindowMessage)LB.SETLOCALE, (IntPtr)CultureInfo.CurrentCulture.LCID);

if (columnWidth != 0)
{
SendMessage((int)LB.SETCOLUMNWIDTH, columnWidth, 0);
SendMessageW(this, (WindowMessage)LB.SETCOLUMNWIDTH, (IntPtr)columnWidth);
}
if (drawMode == DrawMode.OwnerDrawFixed)
{
SendMessage((int)LB.SETITEMHEIGHT, 0, ItemHeight);
SendMessageW(this, (WindowMessage)LB.SETITEMHEIGHT, IntPtr.Zero, (IntPtr)ItemHeight);
}

if (topIndex != 0)
{
SendMessage((int)LB.SETTOPINDEX, topIndex, 0);
SendMessageW(this, (WindowMessage)LB.SETTOPINDEX, (IntPtr)topIndex);
}

if (UseCustomTabOffsets && CustomTabOffsets != null)
{
int wpar = CustomTabOffsets.Count;
int[] offsets = new int[wpar];
CustomTabOffsets.CopyTo(offsets, 0);
UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), (int)LB.SETTABSTOPS, wpar, offsets);

fixed (int* pOffsets = offsets)
{
SendMessageW(this, (WindowMessage)LB.SETTABSTOPS, (IntPtr)wpar, (IntPtr)pOffsets);
}
}

if (itemsCollection != null)
Expand Down Expand Up @@ -1972,18 +1981,21 @@ protected override void OnResize(EventArgs e)
/// </summary>
protected override void OnSelectedIndexChanged(EventArgs e)
{
if (Focused && FocusedItemIsChanged())
if (IsHandleCreated)
{
var focused = AccessibilityObject.GetFocused();
if (focused == AccessibilityObject.GetSelected())
if (Focused && FocusedItemIsChanged())
{
focused?.RaiseAutomationEvent(UiaCore.UIA.SelectionItem_ElementSelectedEventId);
var focused = AccessibilityObject.GetFocused();
if (focused == AccessibilityObject.GetSelected())
{
focused?.RaiseAutomationEvent(UiaCore.UIA.SelectionItem_ElementSelectedEventId);
}
focused?.RaiseAutomationEvent(UiaCore.UIA.AutomationFocusChangedEventId);
}
else if (ItemsCountIsChanged())
{
AccessibilityObject?.GetChild(Items.Count - 1)?.RaiseAutomationEvent(UiaCore.UIA.AutomationFocusChangedEventId);
}
focused?.RaiseAutomationEvent(UiaCore.UIA.AutomationFocusChangedEventId);
}
else if (ItemsCountIsChanged())
{
AccessibilityObject?.GetChild(Items.Count - 1)?.RaiseAutomationEvent(UiaCore.UIA.AutomationFocusChangedEventId);
}

base.OnSelectedIndexChanged(e);
Expand Down Expand Up @@ -2211,7 +2223,7 @@ protected override void SetItemsCore(IList value)

if (IsHandleCreated)
{
SendMessage((int)LB.SETCURSEL, DataManager.Position, 0);
SendMessageW(this, (WindowMessage)LB.SETCURSEL, (IntPtr)DataManager.Position);
}

// if the list changed and we still did not fire the
Expand Down Expand Up @@ -2337,7 +2349,7 @@ private void UpdateHorizontalExtent()
{
width = MaxItemWidth;
}
SendMessage((int)LB.SETHORIZONTALEXTENT, width, 0);
SendMessageW(this, (WindowMessage)LB.SETHORIZONTALEXTENT, (IntPtr)width);
}
}

Expand Down Expand Up @@ -2388,17 +2400,17 @@ private void UpdateMaxItemWidth(object item, bool removing)
}
}

// Updates the Custom TabOffsets
//

private void UpdateCustomTabOffsets()
private unsafe void UpdateCustomTabOffsets()
{
if (IsHandleCreated && UseCustomTabOffsets && CustomTabOffsets != null)
{
int wpar = CustomTabOffsets.Count;
int[] offsets = new int[wpar];
CustomTabOffsets.CopyTo(offsets, 0);
UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), (int)LB.SETTABSTOPS, wpar, offsets);
fixed (int* pOffsets = offsets)
{
SendMessageW(this, (WindowMessage)LB.SETTABSTOPS, (IntPtr)wpar, (IntPtr)pOffsets);
}
Invalidate();
}
}
Expand Down Expand Up @@ -4282,7 +4294,7 @@ public int Count

case SelectionMode.MultiSimple:
case SelectionMode.MultiExtended:
return unchecked((int)(long)owner.SendMessage((int)LB.GETSELCOUNT, 0, 0));
return unchecked((int)(long)SendMessageW(owner, (WindowMessage)LB.GETSELCOUNT));
}

return 0;
Expand Down