| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- #nullable enable
- using System.Collections;
- using System.Data;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("ListColumns", "Implements a columned list via a data table.")]
- [ScenarioCategory ("TableView")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("Dialogs")]
- [ScenarioCategory ("Text and Formatting")]
- [ScenarioCategory ("Scrolling")]
- public class ListColumns : Scenario
- {
- private Scheme? _alternatingScheme;
- private DataTable? _currentTable;
- private TableView? _listColView;
- private CheckBox? _alternatingColorsCheckBox;
- private CheckBox? _alwaysUseNormalColorForVerticalCellLinesCheckBox;
- private CheckBox? _bottomlineCheckBox;
- private CheckBox? _cellLinesCheckBox;
- private CheckBox? _cursorCheckBox;
- private CheckBox? _expandLastColumnCheckBox;
- private CheckBox? _orientVerticalCheckBox;
- private CheckBox? _scrollParallelCheckBox;
- private CheckBox? _smoothScrollingCheckBox;
- private CheckBox? _toplineCheckBox;
- /// <summary>
- /// Builds a simple list in which values are the index. This helps testing that scrolling etc is working
- /// correctly and not skipping out values when paging
- /// </summary>
- /// <param name="items"></param>
- /// <returns></returns>
- public static IList BuildSimpleList (int items)
- {
- List<object> list = [];
- for (var i = 0; i < items; i++)
- {
- list.Add ("Item " + i);
- }
- return list;
- }
- public override void Main ()
- {
- Application.Init ();
- Window appWindow = new ()
- {
- Title = GetQuitKeyAndName (),
- BorderStyle = LineStyle.None
- };
- // MenuBar
- MenuBar menuBar = new ();
- _listColView = new ()
- {
- Y = Pos.Bottom(menuBar),
- Width = Dim.Fill (),
- Height = Dim.Fill (1),
- Style = new ()
- {
- ShowHeaders = false,
- ShowHorizontalHeaderOverline = false,
- ShowHorizontalHeaderUnderline = false,
- ShowHorizontalBottomline = false,
- ExpandLastColumn = false
- }
- };
- ListColumnStyle listColStyle = new ();
- // Status Bar
- StatusBar statusBar = new (
- [
- new (Key.F2, "OpenBigListEx", () => OpenSimpleList (true)),
- new (Key.F3, "CloseExample", CloseExample),
- new (Key.F4, "OpenSmListEx", () => OpenSimpleList (false)),
- new (Application.QuitKey, "Quit", Quit)
- ]
- );
- // Selected cell label
- Label selectedCellLabel = new ()
- {
- X = 0,
- Y = Pos.Bottom (_listColView),
- Text = "0,0",
- Width = Dim.Fill (),
- TextAlignment = Alignment.End
- };
- _listColView.SelectedCellChanged += (s, e) =>
- {
- if (_listColView is { })
- {
- selectedCellLabel.Text = $"{_listColView.SelectedRow},{_listColView.SelectedColumn}";
- }
- };
- _listColView.KeyDown += TableViewKeyPress;
- _alternatingScheme = new ()
- {
- Disabled = appWindow.GetAttributeForRole (VisualRole.Disabled),
- HotFocus = appWindow.GetAttributeForRole (VisualRole.HotFocus),
- Focus = appWindow.GetAttributeForRole (VisualRole.Focus),
- Normal = new (Color.White, Color.BrightBlue)
- };
- // if user clicks the mouse in TableView
- _listColView.MouseClick += (s, e) => { _listColView.ScreenToCell (e.Position, out int? clickedCol); };
- _listColView.KeyBindings.ReplaceCommands (Key.Space, Command.Accept);
- // Setup menu checkboxes
- _toplineCheckBox = new ()
- {
- Title = "_TopLine",
- CheckedState = _listColView.Style.ShowHorizontalHeaderOverline ? CheckState.Checked : CheckState.UnChecked
- };
- _toplineCheckBox.CheckedStateChanged += (s, e) => ToggleTopline ();
- _bottomlineCheckBox = new ()
- {
- Title = "_BottomLine",
- CheckedState = _listColView.Style.ShowHorizontalBottomline ? CheckState.Checked : CheckState.UnChecked
- };
- _bottomlineCheckBox.CheckedStateChanged += (s, e) => ToggleBottomline ();
- _cellLinesCheckBox = new ()
- {
- Title = "_CellLines",
- CheckedState = _listColView.Style.ShowVerticalCellLines ? CheckState.Checked : CheckState.UnChecked
- };
- _cellLinesCheckBox.CheckedStateChanged += (s, e) => ToggleCellLines ();
- _expandLastColumnCheckBox = new ()
- {
- Title = "_ExpandLastColumn",
- CheckedState = _listColView.Style.ExpandLastColumn ? CheckState.Checked : CheckState.UnChecked
- };
- _expandLastColumnCheckBox.CheckedStateChanged += (s, e) => ToggleExpandLastColumn ();
- _alwaysUseNormalColorForVerticalCellLinesCheckBox = new ()
- {
- Title = "_AlwaysUseNormalColorForVerticalCellLines",
- CheckedState = _listColView.Style.AlwaysUseNormalColorForVerticalCellLines ? CheckState.Checked : CheckState.UnChecked
- };
- _alwaysUseNormalColorForVerticalCellLinesCheckBox.CheckedStateChanged += (s, e) => ToggleAlwaysUseNormalColorForVerticalCellLines ();
- _smoothScrollingCheckBox = new ()
- {
- Title = "_SmoothHorizontalScrolling",
- CheckedState = _listColView.Style.SmoothHorizontalScrolling ? CheckState.Checked : CheckState.UnChecked
- };
- _smoothScrollingCheckBox.CheckedStateChanged += (s, e) => ToggleSmoothScrolling ();
- _alternatingColorsCheckBox = new ()
- {
- Title = "Alternating Colors"
- };
- _alternatingColorsCheckBox.CheckedStateChanged += (s, e) => ToggleAlternatingColors ();
- _cursorCheckBox = new ()
- {
- Title = "Invert Selected Cell First Character",
- CheckedState = _listColView.Style.InvertSelectedCellFirstCharacter ? CheckState.Checked : CheckState.UnChecked
- };
- _cursorCheckBox.CheckedStateChanged += (s, e) => ToggleInvertSelectedCellFirstCharacter ();
- _orientVerticalCheckBox = new ()
- {
- Title = "_OrientVertical",
- CheckedState = listColStyle.Orientation == Orientation.Vertical ? CheckState.Checked : CheckState.UnChecked
- };
- _orientVerticalCheckBox.CheckedStateChanged += (s, e) => ToggleVerticalOrientation ();
- _scrollParallelCheckBox = new ()
- {
- Title = "_ScrollParallel",
- CheckedState = listColStyle.ScrollParallel ? CheckState.Checked : CheckState.UnChecked
- };
- _scrollParallelCheckBox.CheckedStateChanged += (s, e) => ToggleScrollParallel ();
- menuBar.Add (
- new MenuBarItem (
- "_File",
- [
- new MenuItem
- {
- Title = "Open_BigListExample",
- Action = () => OpenSimpleList (true)
- },
- new MenuItem
- {
- Title = "Open_SmListExample",
- Action = () => OpenSimpleList (false)
- },
- new MenuItem
- {
- Title = "_CloseExample",
- Action = CloseExample
- },
- new MenuItem
- {
- Title = "_Quit",
- Action = Quit
- }
- ]
- )
- );
- menuBar.Add (
- new MenuBarItem (
- "_View",
- [
- new MenuItem
- {
- CommandView = _toplineCheckBox
- },
- new MenuItem
- {
- CommandView = _bottomlineCheckBox
- },
- new MenuItem
- {
- CommandView = _cellLinesCheckBox
- },
- new MenuItem
- {
- CommandView = _expandLastColumnCheckBox
- },
- new MenuItem
- {
- CommandView = _alwaysUseNormalColorForVerticalCellLinesCheckBox
- },
- new MenuItem
- {
- CommandView = _smoothScrollingCheckBox
- },
- new MenuItem
- {
- CommandView = _alternatingColorsCheckBox
- },
- new MenuItem
- {
- CommandView = _cursorCheckBox
- }
- ]
- )
- );
- menuBar.Add (
- new MenuBarItem (
- "_List",
- [
- new MenuItem
- {
- CommandView = _orientVerticalCheckBox
- },
- new MenuItem
- {
- CommandView = _scrollParallelCheckBox
- },
- new MenuItem
- {
- Title = "Set _Max Cell Width",
- Action = SetListMaxWidth
- },
- new MenuItem
- {
- Title = "Set Mi_n Cell Width",
- Action = SetListMinWidth
- }
- ]
- )
- );
- // Add views in order of visual appearance
- appWindow.Add (menuBar, _listColView, selectedCellLabel, statusBar);
- Application.Run (appWindow);
- appWindow.Dispose ();
- Application.Shutdown ();
- }
- private void CloseExample ()
- {
- if (_listColView is { })
- {
- _listColView.Table = null;
- }
- }
- private void OpenSimpleList (bool big) { SetTable (BuildSimpleList (big ? 1023 : 31)); }
- private void Quit () { Application.RequestStop (); }
- private void RunListWidthDialog (string prompt, Action<TableView, int> setter, Func<TableView, int> getter)
- {
- if (_listColView is null)
- {
- return;
- }
- var accepted = false;
- Button ok = new () { Text = "Ok", IsDefault = true };
- ok.Accepting += (s, e) =>
- {
- accepted = true;
- Application.RequestStop ();
- };
- Button cancel = new () { Text = "Cancel" };
- cancel.Accepting += (s, e) => { Application.RequestStop (); };
- Dialog d = new () { Title = prompt, Buttons = [ok, cancel] };
- TextField tf = new () { Text = getter (_listColView).ToString (), X = 0, Y = 0, Width = Dim.Fill () };
- d.Add (tf);
- tf.SetFocus ();
- Application.Run (d);
- d.Dispose ();
- if (accepted)
- {
- try
- {
- setter (_listColView, int.Parse (tf.Text));
- }
- catch (Exception ex)
- {
- MessageBox.ErrorQuery (Application.Instance, 60, 20, "Failed to set", ex.Message, "Ok");
- }
- }
- }
- private void SetListMaxWidth ()
- {
- RunListWidthDialog ("MaxCellWidth", (s, v) => s.MaxCellWidth = v, s => s.MaxCellWidth);
- _listColView?.SetNeedsDraw ();
- }
- private void SetListMinWidth ()
- {
- RunListWidthDialog ("MinCellWidth", (s, v) => s.MinCellWidth = v, s => s.MinCellWidth);
- _listColView?.SetNeedsDraw ();
- }
- private void SetTable (IList list)
- {
- if (_listColView is null)
- {
- return;
- }
- _listColView.Table = new ListTableSource (list, _listColView);
- if (_listColView.Table is ListTableSource listTableSource)
- {
- _currentTable = listTableSource.DataTable;
- }
- }
- private void TableViewKeyPress (object? sender, Key e)
- {
- if (_currentTable is null || _listColView is null)
- {
- return;
- }
- if (e.KeyCode == Key.Delete)
- {
- // set all selected cells to null
- foreach (Point pt in _listColView.GetAllSelectedCells ())
- {
- _currentTable.Rows [pt.Y] [pt.X] = DBNull.Value;
- }
- _listColView.Update ();
- e.Handled = true;
- }
- }
- private void ToggleAlternatingColors ()
- {
- if (_listColView is null || _alternatingColorsCheckBox is null)
- {
- return;
- }
- if (_alternatingColorsCheckBox.CheckedState == CheckState.Checked)
- {
- _listColView.Style.RowColorGetter = a => a.RowIndex % 2 == 0 ? _alternatingScheme : null;
- }
- else
- {
- _listColView.Style.RowColorGetter = null;
- }
- _listColView.SetNeedsDraw ();
- }
- private void ToggleAlwaysUseNormalColorForVerticalCellLines ()
- {
- if (_listColView is null || _alwaysUseNormalColorForVerticalCellLinesCheckBox is null)
- {
- return;
- }
- _listColView.Style.AlwaysUseNormalColorForVerticalCellLines =
- _alwaysUseNormalColorForVerticalCellLinesCheckBox.CheckedState == CheckState.Checked;
- _listColView.Update ();
- }
- private void ToggleBottomline ()
- {
- if (_listColView is null || _bottomlineCheckBox is null)
- {
- return;
- }
- _listColView.Style.ShowHorizontalBottomline = _bottomlineCheckBox.CheckedState == CheckState.Checked;
- _listColView.Update ();
- }
- private void ToggleCellLines ()
- {
- if (_listColView is null || _cellLinesCheckBox is null)
- {
- return;
- }
- _listColView.Style.ShowVerticalCellLines = _cellLinesCheckBox.CheckedState == CheckState.Checked;
- _listColView.Update ();
- }
- private void ToggleExpandLastColumn ()
- {
- if (_listColView is null || _expandLastColumnCheckBox is null)
- {
- return;
- }
- _listColView.Style.ExpandLastColumn = _expandLastColumnCheckBox.CheckedState == CheckState.Checked;
- _listColView.Update ();
- }
- private void ToggleInvertSelectedCellFirstCharacter ()
- {
- if (_listColView is null || _cursorCheckBox is null)
- {
- return;
- }
- _listColView.Style.InvertSelectedCellFirstCharacter = _cursorCheckBox.CheckedState == CheckState.Checked;
- _listColView.SetNeedsDraw ();
- }
- private void ToggleScrollParallel ()
- {
- if (_listColView?.Table is not ListTableSource listTableSource || _scrollParallelCheckBox is null)
- {
- return;
- }
- listTableSource.Style.ScrollParallel = _scrollParallelCheckBox.CheckedState == CheckState.Checked;
- _listColView.SetNeedsDraw ();
- }
- private void ToggleSmoothScrolling ()
- {
- if (_listColView is null || _smoothScrollingCheckBox is null)
- {
- return;
- }
- _listColView.Style.SmoothHorizontalScrolling = _smoothScrollingCheckBox.CheckedState == CheckState.Checked;
- _listColView.Update ();
- }
- private void ToggleTopline ()
- {
- if (_listColView is null || _toplineCheckBox is null)
- {
- return;
- }
- _listColView.Style.ShowHorizontalHeaderOverline = _toplineCheckBox.CheckedState == CheckState.Checked;
- _listColView.Update ();
- }
- private void ToggleVerticalOrientation ()
- {
- if (_listColView?.Table is not ListTableSource listTableSource || _orientVerticalCheckBox is null)
- {
- return;
- }
- listTableSource.Style.Orientation = _orientVerticalCheckBox.CheckedState == CheckState.Checked
- ? Orientation.Vertical
- : Orientation.Horizontal;
- _listColView.SetNeedsDraw ();
- }
- }
|