//
// ComboBox.cs: ComboBox control
//
// Authors:
// Ross Ferguson (ross.c.ferguson@btinternet.com)
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NStack;
namespace Terminal.Gui {
///
/// ComboBox control
///
public class ComboBox : View {
IListDataSource source;
///
/// Gets or sets the backing this , enabling custom rendering.
///
/// The source.
///
/// Use to set a new source.
///
public IListDataSource Source {
get => source;
set {
source = value;
Search_Changed ("");
SetNeedsDisplay ();
}
}
///
/// Sets the source of the to an .
///
/// An object implementing the IList interface.
///
/// Use the property to set a new source and use custome rendering.
///
public void SetSource (IList source)
{
if (source == null) {
Source = null;
} else {
Source = MakeWrapper (source);
}
}
///
/// Changed event, raised when the selection has been confirmed.
///
///
/// Client code can hook up to this event, it is
/// raised when the selection has been confirmed.
///
public event EventHandler SelectedItemChanged;
IList searchset;
ustring text = "";
readonly TextField search;
readonly ListView listview;
int height;
int width;
bool autoHide = true;
///
/// Public constructor
///
public ComboBox () : base ()
{
search = new TextField ("");
listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
Initialize ();
}
///
/// Public constructor
///
///
public ComboBox (ustring text) : base ()
{
search = new TextField ("");
listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
Initialize ();
Text = text;
}
///
/// Public constructor
///
///
public ComboBox (ustring text) : base ()
{
search = new TextField ("");
listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
Initialize ();
Text = text;
}
///
/// Public constructor
///
///
///
public ComboBox (Rect rect, IList source) : base (rect)
{
this.height = rect.Height;
this.width = rect.Width;
search = new TextField ("") { Width = width };
listview = new ListView (rect, source) { LayoutStyle = LayoutStyle.Computed };
Initialize ();
SetSource (source);
}
static IListDataSource MakeWrapper (IList source)
{
return new ListWrapper (source);
}
private void Initialize ()
{
ColorScheme = Colors.Base;
search.TextChanged += Search_Changed;
listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected ();
// On resize
LayoutComplete += (LayoutEventArgs a) => {
search.Width = Bounds.Width;
listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
listview.Height = CalculatetHeight ();
};
listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
if (searchset.Count > 0) {
SetValue ((ustring)searchset [listview.SelectedItem]);
}
};
Application.Loaded += (Application.ResizedEventArgs a) => {
// Determine if this view is hosted inside a dialog
for (View view = this.SuperView; view != null; view = view.SuperView) {
if (view is Dialog) {
autoHide = false;
break;
}
}
ResetSearchSet ();
ColorScheme = autoHide ? Colors.Base : ColorScheme = null;
listview.Y = Pos.Bottom (search);
if (Width != null && width == 0) { // new ComboBox() { Width =
width = Bounds.Width;
}
search.Width = width;
listview.Width = CalculateWidth ();
if (Height != null && height == 0) { // new ComboBox() { Height =
height = Bounds.Height;
}
listview.Height = CalculatetHeight ();
SetNeedsLayout ();
if (this.Text != null) {
Search_Changed (Text);
}
if (autoHide) {
listview.ColorScheme = Colors.Menu;
} else {
search.ColorScheme = Colors.Menu;
}
};
search.MouseClick += Search_MouseClick;
this.Add (listview, search);
this.SetFocus (search);
}
#if COMBO_FEATURE
bool isShow = false;
#endif
private void Search_MouseClick (MouseEventArgs me)
{
#if !COMBO_FEATURE
if (me.MouseEvent.Flags != MouseFlags.Button1Clicked)
return;
#else
if (me.MouseEvent.X == Bounds.Right - 1 && me.MouseEvent.Y == Bounds.Top && me.MouseEvent.Flags == MouseFlags.Button1Pressed
&& search.Text == "" && autoHide) {
if (isShow) {
HideList ();
isShow = false;
} else {
searchset = Source.ToList().Cast