1
0
mirror of https://github.com/Kopano-dev/kopano-ol-extension.git synced 2023-10-10 13:37:40 +02:00

Clean ups to combobox

This commit is contained in:
Patrick Simpson 2017-06-29 13:07:14 +02:00
parent af97aae748
commit fdf8350afb
2 changed files with 36 additions and 33 deletions

View File

@ -14,31 +14,35 @@ namespace Acacia.Controls
{ {
public class KComboBox : KAbstractComboBox public class KComboBox : KAbstractComboBox
{ {
private class KListBox : ListBox #region Drop-down list
private class DropList : ListBox
{ {
private readonly KComboBox _owner; private readonly KComboBox _owner;
private int _hoverIndex = -1; private int _highlightIndex = -1;
public KListBox(KComboBox owner) public DropList(KComboBox owner)
{ {
this._owner = owner; this._owner = owner;
SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
DrawMode = DrawMode.OwnerDrawFixed; DrawMode = DrawMode.OwnerDrawFixed;
SetStyle(ControlStyles.Selectable, false); SetStyle(ControlStyles.Selectable, false);
//ItemHeight = 23;
BorderStyle = BorderStyle.None; BorderStyle = BorderStyle.None;
} }
protected override void OnMouseMove(MouseEventArgs e) protected override void OnMouseMove(MouseEventArgs e)
{ {
// Use the mouse to highlight the current item
int newIndex = IndexFromPoint(PointToClient(Cursor.Position)); int newIndex = IndexFromPoint(PointToClient(Cursor.Position));
if (newIndex != _hoverIndex) if (newIndex != _highlightIndex)
{ {
int oldIndex = _hoverIndex; int oldIndex = _highlightIndex;
_hoverIndex = newIndex; _highlightIndex = newIndex;
// Invalidate the affected items, which may include a previously selected one
InvalidateItem(oldIndex); InvalidateItem(oldIndex);
InvalidateItem(_hoverIndex); InvalidateItem(_highlightIndex);
if (SelectedIndex != oldIndex && SelectedIndex != _hoverIndex) if (SelectedIndex != oldIndex && SelectedIndex != _highlightIndex)
InvalidateItem(SelectedIndex); InvalidateItem(SelectedIndex);
} }
} }
@ -46,21 +50,21 @@ namespace Acacia.Controls
protected override void OnMouseLeave(EventArgs e) protected override void OnMouseLeave(EventArgs e)
{ {
base.OnMouseLeave(e); base.OnMouseLeave(e);
_hoverIndex = -1; _highlightIndex = -1;
} }
protected override void OnMouseDown(MouseEventArgs e) protected override void OnMouseDown(MouseEventArgs e)
{ {
base.OnMouseDown(e); base.OnMouseDown(e);
// Perform the select here.
// TODO: this really is for ComboBox, where the list hides before the event is handled // Perform the select when the mouse is clicked
SelectedIndex = IndexFromPoint(PointToClient(Cursor.Position)); SelectedIndex = IndexFromPoint(PointToClient(Cursor.Position));
} }
protected override void OnVisibleChanged(EventArgs e) protected override void OnVisibleChanged(EventArgs e)
{ {
base.OnVisibleChanged(e); base.OnVisibleChanged(e);
_hoverIndex = -1; _highlightIndex = -1;
} }
private void InvalidateItem(int index) private void InvalidateItem(int index)
@ -74,9 +78,9 @@ namespace Acacia.Controls
{ {
// Create a custom event instance to be able to set the selected state for mouse hover // Create a custom event instance to be able to set the selected state for mouse hover
DrawItemState state = e.State; DrawItemState state = e.State;
if (_hoverIndex >= 0) if (_highlightIndex >= 0)
{ {
state = _hoverIndex == e.Index ? DrawItemState.Selected : DrawItemState.None; state = _highlightIndex == e.Index ? DrawItemState.Selected : DrawItemState.None;
} }
DrawItemEventArgs draw = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, state); DrawItemEventArgs draw = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, state);
draw.DrawBackground(); draw.DrawBackground();
@ -97,14 +101,11 @@ namespace Acacia.Controls
protected override void DefWndProc(ref Message m) protected override void DefWndProc(ref Message m)
{ {
const int WM_MOUSEACTIVATE = 0x21; switch ((WM)m.Msg)
const int MA_NOACTIVATE = 0x0003;
switch (m.Msg)
{ {
// Prevent mouse activity from grabbing the focus away from the edit // Prevent mouse activity from grabbing the focus away from the edit
case WM_MOUSEACTIVATE: case WM.MOUSEACTIVATE:
m.Result = (IntPtr)MA_NOACTIVATE; m.Result = (IntPtr)MA.NOACTIVATE;
return; return;
} }
base.DefWndProc(ref m); base.DefWndProc(ref m);
@ -112,12 +113,15 @@ namespace Acacia.Controls
public override Size GetPreferredSize(Size proposedSize) public override Size GetPreferredSize(Size proposedSize)
{ {
// Preferred size is simply the size of the (maximum) number of items
Size prefSize = base.GetPreferredSize(proposedSize); Size prefSize = base.GetPreferredSize(proposedSize);
return new Size(prefSize.Width, ItemHeight * _owner.MaxDropDownItems); return new Size(prefSize.Width, ItemHeight * Math.Min(Items.Count, _owner.MaxDropDownItems));
} }
} }
private readonly KListBox _list; #endregion
private readonly DropList _list;
private int _ignoreListEvents; private int _ignoreListEvents;
#region Items properties #region Items properties
@ -149,19 +153,11 @@ namespace Acacia.Controls
public KComboBox() public KComboBox()
{ {
MaxDropDownItems = 8; MaxDropDownItems = 8;
_list = new KListBox(this); _list = new DropList(this);
_list.IntegralHeight = true; _list.IntegralHeight = true;
_list.TabStop = false; _list.TabStop = false;
DropControl = _list;
_list.DisplayMember = "DisplayName"; // TODO: remove from here
_list.SelectedIndexChanged += _list_SelectedIndexChanged; _list.SelectedIndexChanged += _list_SelectedIndexChanged;
_list.GotFocus += _list_GotFocus; DropControl = _list;
}
private void _list_GotFocus(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("_list_GotFocus");
} }
private void _list_SelectedIndexChanged(object sender, EventArgs e) private void _list_SelectedIndexChanged(object sender, EventArgs e)

View File

@ -23,8 +23,15 @@ using System.Text;
namespace Acacia.Native namespace Acacia.Native
{ {
public enum MA : int
{
NOACTIVATE = 0x0003
}
public enum WM : int public enum WM : int
{ {
MOUSEACTIVATE = 0x0021,
NCHITTEST = 0x0084, NCHITTEST = 0x0084,
NCPAINT = 0x0085, NCPAINT = 0x0085,