Пример #1
0
        public DynamicStatusBarSample(ustring title) : base(title)
        {
            DataContext = new DynamicStatusItemModel();

            var _frmDelimiter = new FrameView("Shortcut Delimiter:")
            {
                X      = Pos.Center(),
                Y      = 0,
                Width  = 25,
                Height = 4
            };

            var _txtDelimiter = new TextField(StatusBar.ShortcutDelimiter.ToString())
            {
                X     = Pos.Center(),
                Width = 2,
            };

            _txtDelimiter.TextChanged += (_) => StatusBar.ShortcutDelimiter = _txtDelimiter.Text;
            _frmDelimiter.Add(_txtDelimiter);

            Add(_frmDelimiter);

            var _frmStatusBar = new FrameView("Items:")
            {
                Y      = 5,
                Width  = Dim.Percent(50),
                Height = Dim.Fill(2)
            };

            var _btnAddStatusBar = new Button("Add a StatusBar")
            {
                Y = 1,
            };

            _frmStatusBar.Add(_btnAddStatusBar);

            var _btnRemoveStatusBar = new Button("Remove a StatusBar")
            {
                Y = 1
            };

            _btnRemoveStatusBar.X = Pos.AnchorEnd() - (Pos.Right(_btnRemoveStatusBar) - Pos.Left(_btnRemoveStatusBar));
            _frmStatusBar.Add(_btnRemoveStatusBar);

            var _btnAdd = new Button(" Add  ")
            {
                Y = Pos.Top(_btnRemoveStatusBar) + 2,
            };

            _btnAdd.X = Pos.AnchorEnd() - (Pos.Right(_btnAdd) - Pos.Left(_btnAdd));
            _frmStatusBar.Add(_btnAdd);

            _lstItems = new ListView(new List <DynamicStatusItemList> ())
            {
                ColorScheme = Colors.Dialog,
                Y           = Pos.Top(_btnAddStatusBar) + 2,
                Width       = Dim.Fill() - Dim.Width(_btnAdd) - 1,
                Height      = Dim.Fill(),
            };
            _frmStatusBar.Add(_lstItems);

            var _btnRemove = new Button("Remove")
            {
                X = Pos.Left(_btnAdd),
                Y = Pos.Top(_btnAdd) + 1
            };

            _frmStatusBar.Add(_btnRemove);

            var _btnUp = new Button("^")
            {
                X = Pos.Right(_lstItems) + 2,
                Y = Pos.Top(_btnRemove) + 2
            };

            _frmStatusBar.Add(_btnUp);

            var _btnDown = new Button("v")
            {
                X = Pos.Right(_lstItems) + 2,
                Y = Pos.Top(_btnUp) + 1
            };

            _frmStatusBar.Add(_btnDown);

            Add(_frmStatusBar);


            var _frmStatusBarDetails = new DynamicStatusBarDetails("StatusBar Item Details:")
            {
                X      = Pos.Right(_frmStatusBar),
                Y      = Pos.Top(_frmStatusBar),
                Width  = Dim.Fill(),
                Height = Dim.Fill(4)
            };

            Add(_frmStatusBarDetails);

            _btnUp.Clicked += () => {
                var i          = _lstItems.SelectedItem;
                var statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].StatusItem : null;
                if (statusItem != null)
                {
                    var items = _statusBar.Items;
                    if (i > 0)
                    {
                        items [i]                 = items [i - 1];
                        items [i - 1]             = statusItem;
                        DataContext.Items [i]     = DataContext.Items [i - 1];
                        DataContext.Items [i - 1] = new DynamicStatusItemList(statusItem.Title, statusItem);
                        _lstItems.SelectedItem    = i - 1;
                        _statusBar.SetNeedsDisplay();
                    }
                }
            };

            _btnDown.Clicked += () => {
                var i          = _lstItems.SelectedItem;
                var statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].StatusItem : null;
                if (statusItem != null)
                {
                    var items = _statusBar.Items;
                    if (i < items.Length - 1)
                    {
                        items [i]                 = items [i + 1];
                        items [i + 1]             = statusItem;
                        DataContext.Items [i]     = DataContext.Items [i + 1];
                        DataContext.Items [i + 1] = new DynamicStatusItemList(statusItem.Title, statusItem);
                        _lstItems.SelectedItem    = i + 1;
                        _statusBar.SetNeedsDisplay();
                    }
                }
            };

            var _btnOk = new Button("Ok")
            {
                X = Pos.Right(_frmStatusBar) + 20,
                Y = Pos.Bottom(_frmStatusBarDetails),
            };

            Add(_btnOk);

            var _btnCancel = new Button("Cancel")
            {
                X = Pos.Right(_btnOk) + 3,
                Y = Pos.Top(_btnOk),
            };

            _btnCancel.Clicked += () => {
                SetFrameDetails(_currentEditStatusItem);
            };
            Add(_btnCancel);

            _lstItems.SelectedItemChanged += (e) => {
                SetFrameDetails();
            };

            _btnOk.Clicked += () => {
                if (ustring.IsNullOrEmpty(_frmStatusBarDetails._txtTitle.Text) && _currentEditStatusItem != null)
                {
                    MessageBox.ErrorQuery("Invalid title", "Must enter a valid title!.", "Ok");
                }
                else if (_currentEditStatusItem != null)
                {
                    _frmStatusBarDetails._txtTitle.Text = SetTitleText(
                        _frmStatusBarDetails._txtTitle.Text, _frmStatusBarDetails._txtShortcut.Text);
                    var statusItem = new DynamicStatusItem(_frmStatusBarDetails._txtTitle.Text,
                                                           _frmStatusBarDetails._txtAction.Text,
                                                           _frmStatusBarDetails._txtShortcut.Text);
                    UpdateStatusItem(_currentEditStatusItem, statusItem, _lstItems.SelectedItem);
                }
            };

            _btnAdd.Clicked += () => {
                if (StatusBar == null)
                {
                    MessageBox.ErrorQuery("StatusBar Bar Error", "Must add a StatusBar first!", "Ok");
                    _btnAddStatusBar.SetFocus();
                    return;
                }

                var frameDetails = new DynamicStatusBarDetails();
                var item         = frameDetails.EnterStatusItem();
                if (item == null)
                {
                    return;
                }

                StatusItem newStatusItem = CreateNewStatusBar(item);
                _currentSelectedStatusBar++;
                _statusBar.AddItemAt(_currentSelectedStatusBar, newStatusItem);
                DataContext.Items.Add(new DynamicStatusItemList(newStatusItem.Title, newStatusItem));
                _lstItems.MoveDown();
            };

            _btnRemove.Clicked += () => {
                var statusItem = DataContext.Items.Count > 0 ? DataContext.Items [_lstItems.SelectedItem].StatusItem : null;
                if (statusItem != null)
                {
                    _statusBar.RemoveItem(_currentSelectedStatusBar);
                    DataContext.Items.RemoveAt(_lstItems.SelectedItem);
                    if (_lstItems.Source.Count > 0 && _lstItems.SelectedItem > _lstItems.Source.Count - 1)
                    {
                        _lstItems.SelectedItem = _lstItems.Source.Count - 1;
                    }
                    _lstItems.SetNeedsDisplay();
                    SetFrameDetails();
                }
            };

            _lstItems.Enter += (_) => {
                var statusItem = DataContext.Items.Count > 0 ? DataContext.Items [_lstItems.SelectedItem].StatusItem : null;
                SetFrameDetails(statusItem);
            };

            _btnAddStatusBar.Clicked += () => {
                if (_statusBar != null)
                {
                    return;
                }

                _statusBar = new StatusBar();
                Add(_statusBar);
            };

            _btnRemoveStatusBar.Clicked += () => {
                if (_statusBar == null)
                {
                    return;
                }

                Remove(_statusBar);
                _statusBar                = null;
                DataContext.Items         = new List <DynamicStatusItemList> ();
                _currentStatusItem        = null;
                _currentSelectedStatusBar = -1;
                SetListViewSource(_currentStatusItem, true);
                SetFrameDetails(null);
            };


            SetFrameDetails();


            var ustringConverter     = new UStringValueConverter();
            var listWrapperConverter = new ListWrapperConverter();

            var lstItems = new Binding(this, "Items", _lstItems, "Source", listWrapperConverter);


            void SetFrameDetails(StatusItem statusItem = null)
            {
                StatusItem newStatusItem;

                if (statusItem == null)
                {
                    newStatusItem = DataContext.Items.Count > 0 ? DataContext.Items [_lstItems.SelectedItem].StatusItem : null;
                }
                else
                {
                    newStatusItem = statusItem;
                }

                _currentEditStatusItem = newStatusItem;
                _frmStatusBarDetails.EditStatusItem(newStatusItem);
                var f = _btnOk.Enabled == _frmStatusBarDetails.Enabled;

                if (!f)
                {
                    _btnOk.Enabled     = _frmStatusBarDetails.Enabled;
                    _btnCancel.Enabled = _frmStatusBarDetails.Enabled;
                }
            }

            void SetListViewSource(StatusItem _currentStatusItem, bool fill = false)
            {
                DataContext.Items = new List <DynamicStatusItemList> ();
                var statusItem = _currentStatusItem;

                if (!fill)
                {
                    return;
                }
                if (statusItem != null)
                {
                    foreach (var si in _statusBar.Items)
                    {
                        DataContext.Items.Add(new DynamicStatusItemList(si.Title, si));
                    }
                }
            }

            StatusItem CreateNewStatusBar(DynamicStatusItem item)
            {
                StatusItem newStatusItem;

                newStatusItem = new StatusItem(ShortcutHelper.GetShortcutFromTag(
                                                   item.shortcut, StatusBar.ShortcutDelimiter),
                                               item.title, _frmStatusBarDetails.CreateAction(item));

                return(newStatusItem);
            }

            void UpdateStatusItem(StatusItem _currentEditStatusItem, DynamicStatusItem statusItem, int index)
            {
                _currentEditStatusItem   = CreateNewStatusBar(statusItem);
                _statusBar.Items [index] = _currentEditStatusItem;
                if (DataContext.Items.Count == 0)
                {
                    DataContext.Items.Add(new DynamicStatusItemList(_currentEditStatusItem.Title, _currentEditStatusItem));
                }
                DataContext.Items [index] = new DynamicStatusItemList(_currentEditStatusItem.Title, _currentEditStatusItem);
                SetFrameDetails(_currentEditStatusItem);
            }

            //_frmStatusBarDetails.Initialized += (s, e) => _frmStatusBarDetails.Enabled = false;
        }