Exemple #1
0
        public ScrollBarViewTests()
        {
            Application.Init(new FakeDriver(), new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var top = Application.Top;

            _hostView = new HostView()
            {
                Width  = Dim.Fill(),
                Height = Dim.Fill(),
                Top    = 0,
                Lines  = 30,
                Left   = 0,
                Cols   = 100
            };

            top.Add(_hostView);
        }
Exemple #2
0
 internal void PositionToplevels()
 {
     if (this != Application.Top)
     {
         EnsureVisibleBounds(this, Frame.X, Frame.Y, out int nx, out int ny);
         if ((nx != Frame.X || ny != Frame.Y) && LayoutStyle != LayoutStyle.Computed)
         {
             X = nx;
             Y = ny;
         }
     }
     else
     {
         foreach (var top in Subviews)
         {
             if (top is Toplevel)
             {
                 EnsureVisibleBounds((Toplevel)top, top.Frame.X, top.Frame.Y, out int nx, out int ny);
                 if ((nx != top.Frame.X || ny != top.Frame.Y) && top.LayoutStyle != LayoutStyle.Computed)
                 {
                     top.X = nx;
                     top.Y = ny;
                 }
                 if (StatusBar != null)
                 {
                     if (ny + top.Frame.Height > Driver.Rows - 1)
                     {
                         if (top.Height is Dim.DimFill)
                         {
                             top.Height = Dim.Fill() - 1;
                         }
                     }
                     if (StatusBar.Frame.Y != Driver.Rows - 1)
                     {
                         StatusBar.Y = Driver.Rows - 1;
                         SetNeedsDisplay();
                     }
                 }
             }
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of <see cref="StatusItem"/>s.
        /// The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or <see cref="StatusBar.Parent"/> (if not null).
        /// </summary>
        /// <param name="items">A list of statusbar items.</param>
        public StatusBar(StatusItem [] items) : base()
        {
            Width       = Dim.Fill();
            Height      = 1;
            Items       = items;
            CanFocus    = false;
            ColorScheme = Colors.Menu;
            X           = 0;
            Y           = Driver.Rows - 1;
            Width       = Dim.Fill();
            Height      = 1;

            Application.Loaded += (sender, e) => {
                X      = 0;
                Height = 1;
#if SNAP_TO_TOP
                switch (Style)
                {
                case StatusBarStyle.SnapToTop:
                    X = 0;
                    Y = 0;
                    break;

                case StatusBarStyle.SnapToBottom:
#endif
                if (Parent == null)
                {
                    Y = e.Rows - 1;
                }
                else
                {
                    Y = Pos.Bottom(Parent);
                }
#if SNAP_TO_TOP
                break;
            }
#endif
            };
        }
Exemple #4
0
        public FileDialog(ustring title, ustring prompt, ustring nameFieldLabel, ustring message) : base(title, Driver.Cols - 20, Driver.Rows - 6, null)
        {
            this.message = new Label(Rect.Empty, message);
            var msgLines = Label.MeasureLines(message, Driver.Cols - 20);

            dirLabel = new Label("Directory: ")
            {
                X = 2,
                Y = 1 + msgLines
            };

            dirEntry = new TextField("")
            {
                X = 12,
                Y = 1 + msgLines
            };
            Add(dirLabel, dirEntry);

            this.nameFieldLabel = new Label(nameFieldLabel)
            {
                X = 2,
                Y = 3 + msgLines,
            };
            nameEntry = new TextField("")
            {
                X     = 2 + nameFieldLabel.RuneCount + 1,
                Y     = 3 + msgLines,
                Width = Dim.Fill() - 1
            };
            Add(this.nameFieldLabel, nameEntry);

            this.cancel = new Button("Cancel");
            AddButton(cancel);

            this.prompt = new Button(prompt);
            AddButton(this.prompt);
        }
Exemple #5
0
        /// <summary>
        /// Creates a horizontal or vertical line based on <paramref name="orientation"/>
        /// </summary>
        public LineView(Orientation orientation)
        {
            CanFocus = false;

            switch (orientation)
            {
            case Orientation.Horizontal:
                Height   = 1;
                Width    = Dim.Fill();
                LineRune = Driver.HLine;

                break;

            case Orientation.Vertical:
                Height   = Dim.Fill();
                Width    = 1;
                LineRune = Driver.VLine;
                break;

            default:
                throw new ArgumentException($"Unknown Orientation {orientation}");
            }
            Orientation = orientation;
        }
Exemple #6
0
 private void PositionToplevel(Toplevel top)
 {
     EnsureVisibleBounds(top, top.Frame.X, top.Frame.Y, out int nx, out int ny);
     if ((nx != top.Frame.X || ny != top.Frame.Y) && top.LayoutStyle != LayoutStyle.Computed)
     {
         top.X = nx;
         top.Y = ny;
     }
     if (StatusBar != null)
     {
         if (ny + top.Frame.Height > Driver.Rows - 1)
         {
             if (top.Height is Dim.DimFill)
             {
                 top.Height = Dim.Fill() - 1;
             }
         }
         if (StatusBar.Frame.Y != Driver.Rows - 1)
         {
             StatusBar.Y = Driver.Rows - 1;
             SetNeedsDisplay();
         }
     }
 }
        public void Run_All_Views_Tester_Scenario()
        {
            Window    _leftPane;
            ListView  _classListView;
            FrameView _hostPane;

            Dictionary <string, Type> _viewClasses;
            View _curView = null;

            // Settings
            FrameView  _settingsPane;
            CheckBox   _computedCheckBox;
            FrameView  _locationFrame;
            RadioGroup _xRadioGroup;
            TextField  _xText;
            int        _xVal = 0;
            RadioGroup _yRadioGroup;
            TextField  _yText;
            int        _yVal = 0;

            FrameView     _sizeFrame;
            RadioGroup    _wRadioGroup;
            TextField     _wText;
            int           _wVal = 0;
            RadioGroup    _hRadioGroup;
            TextField     _hText;
            int           _hVal    = 0;
            List <string> posNames = new List <String> {
                "Factor", "AnchorEnd", "Center", "Absolute"
            };
            List <string> dimNames = new List <String> {
                "Factor", "Fill", "Absolute"
            };


            Application.Init(new FakeDriver(), new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var Top = Application.Top;

            _viewClasses = GetAllViewClassesCollection()
                           .OrderBy(t => t.Name)
                           .Select(t => new KeyValuePair <string, Type> (t.Name, t))
                           .ToDictionary(t => t.Key, t => t.Value);

            _leftPane = new Window("Classes")
            {
                X           = 0,
                Y           = 0,
                Width       = 15,
                Height      = Dim.Fill(1),             // for status bar
                CanFocus    = false,
                ColorScheme = Colors.TopLevel,
            };

            _classListView = new ListView(_viewClasses.Keys.ToList())
            {
                X             = 0,
                Y             = 0,
                Width         = Dim.Fill(0),
                Height        = Dim.Fill(0),
                AllowsMarking = false,
                ColorScheme   = Colors.TopLevel,
            };
            _leftPane.Add(_classListView);

            _settingsPane = new FrameView("Settings")
            {
                X           = Pos.Right(_leftPane),
                Y           = 0,       // for menu
                Width       = Dim.Fill(),
                Height      = 10,
                CanFocus    = false,
                ColorScheme = Colors.TopLevel,
            };
            _computedCheckBox = new CheckBox("Computed Layout", true)
            {
                X = 0, Y = 0
            };
            _settingsPane.Add(_computedCheckBox);

            var radioItems = new ustring [] { "Percent(x)", "AnchorEnd(x)", "Center", "At(x)" };

            _locationFrame = new FrameView("Location (Pos)")
            {
                X      = Pos.Left(_computedCheckBox),
                Y      = Pos.Bottom(_computedCheckBox),
                Height = 3 + radioItems.Length,
                Width  = 36,
            };
            _settingsPane.Add(_locationFrame);

            var label = new Label("x:")
            {
                X = 0, Y = 0
            };

            _locationFrame.Add(label);
            _xRadioGroup = new RadioGroup(radioItems)
            {
                X = 0,
                Y = Pos.Bottom(label),
            };
            _xText = new TextField($"{_xVal}")
            {
                X = Pos.Right(label) + 1, Y = 0, Width = 4
            };
            _locationFrame.Add(_xText);

            _locationFrame.Add(_xRadioGroup);

            radioItems = new ustring [] { "Percent(y)", "AnchorEnd(y)", "Center", "At(y)" };
            label      = new Label("y:")
            {
                X = Pos.Right(_xRadioGroup) + 1, Y = 0
            };
            _locationFrame.Add(label);
            _yText = new TextField($"{_yVal}")
            {
                X = Pos.Right(label) + 1, Y = 0, Width = 4
            };
            _locationFrame.Add(_yText);
            _yRadioGroup = new RadioGroup(radioItems)
            {
                X = Pos.X(label),
                Y = Pos.Bottom(label),
            };
            _locationFrame.Add(_yRadioGroup);

            _sizeFrame = new FrameView("Size (Dim)")
            {
                X      = Pos.Right(_locationFrame),
                Y      = Pos.Y(_locationFrame),
                Height = 3 + radioItems.Length,
                Width  = 40,
            };

            radioItems = new ustring [] { "Percent(width)", "Fill(width)", "Sized(width)" };
            label      = new Label("width:")
            {
                X = 0, Y = 0
            };
            _sizeFrame.Add(label);
            _wRadioGroup = new RadioGroup(radioItems)
            {
                X = 0,
                Y = Pos.Bottom(label),
            };
            _wText = new TextField($"{_wVal}")
            {
                X = Pos.Right(label) + 1, Y = 0, Width = 4
            };
            _sizeFrame.Add(_wText);
            _sizeFrame.Add(_wRadioGroup);

            radioItems = new ustring [] { "Percent(height)", "Fill(height)", "Sized(height)" };
            label      = new Label("height:")
            {
                X = Pos.Right(_wRadioGroup) + 1, Y = 0
            };
            _sizeFrame.Add(label);
            _hText = new TextField($"{_hVal}")
            {
                X = Pos.Right(label) + 1, Y = 0, Width = 4
            };
            _sizeFrame.Add(_hText);

            _hRadioGroup = new RadioGroup(radioItems)
            {
                X = Pos.X(label),
                Y = Pos.Bottom(label),
            };
            _sizeFrame.Add(_hRadioGroup);

            _settingsPane.Add(_sizeFrame);

            _hostPane = new FrameView("")
            {
                X           = Pos.Right(_leftPane),
                Y           = Pos.Bottom(_settingsPane),
                Width       = Dim.Fill(),
                Height      = Dim.Fill(1),             // + 1 for status bar
                ColorScheme = Colors.Dialog,
            };

            _classListView.OpenSelectedItem += (a) => {
                _settingsPane.SetFocus();
            };
            _classListView.SelectedItemChanged += (args) => {
                ClearClass(_curView);
                _curView = CreateClass(_viewClasses.Values.ToArray() [_classListView.SelectedItem]);
            };

            _computedCheckBox.Toggled += (previousState) => {
                if (_curView != null)
                {
                    _curView.LayoutStyle = previousState ? LayoutStyle.Absolute : LayoutStyle.Computed;
                    _hostPane.LayoutSubviews();
                }
            };

            _xRadioGroup.SelectedItemChanged += (selected) => DimPosChanged(_curView);

            _xText.TextChanged += (args) => {
                try {
                    _xVal = int.Parse(_xText.Text.ToString());
                    DimPosChanged(_curView);
                } catch {
                }
            };

            _yText.TextChanged += (args) => {
                try {
                    _yVal = int.Parse(_yText.Text.ToString());
                    DimPosChanged(_curView);
                } catch {
                }
            };

            _yRadioGroup.SelectedItemChanged += (selected) => DimPosChanged(_curView);

            _wRadioGroup.SelectedItemChanged += (selected) => DimPosChanged(_curView);

            _wText.TextChanged += (args) => {
                try {
                    _wVal = int.Parse(_wText.Text.ToString());
                    DimPosChanged(_curView);
                } catch {
                }
            };

            _hText.TextChanged += (args) => {
                try {
                    _hVal = int.Parse(_hText.Text.ToString());
                    DimPosChanged(_curView);
                } catch {
                }
            };

            _hRadioGroup.SelectedItemChanged += (selected) => DimPosChanged(_curView);

            Top.Add(_leftPane, _settingsPane, _hostPane);

            Top.LayoutSubviews();

            _curView = CreateClass(_viewClasses.First().Value);

            int iterations = 0;

            Application.Iteration += () => {
                iterations++;

                if (iterations < _viewClasses.Count)
                {
                    _classListView.MoveDown();
                    Assert.Equal(_curView.GetType().Name,
                                 _viewClasses.Values.ToArray() [_classListView.SelectedItem].Name);
                }
                else
                {
                    Application.RequestStop();
                }
            };

            Application.Run();

            Assert.Equal(_viewClasses.Count, iterations);

            Application.Shutdown();


            void DimPosChanged(View view)
            {
                if (view == null)
                {
                    return;
                }

                var layout = view.LayoutStyle;

                try {
                    view.LayoutStyle = LayoutStyle.Absolute;

                    switch (_xRadioGroup.SelectedItem)
                    {
                    case 0:
                        view.X = Pos.Percent(_xVal);
                        break;

                    case 1:
                        view.X = Pos.AnchorEnd(_xVal);
                        break;

                    case 2:
                        view.X = Pos.Center();
                        break;

                    case 3:
                        view.X = Pos.At(_xVal);
                        break;
                    }

                    switch (_yRadioGroup.SelectedItem)
                    {
                    case 0:
                        view.Y = Pos.Percent(_yVal);
                        break;

                    case 1:
                        view.Y = Pos.AnchorEnd(_yVal);
                        break;

                    case 2:
                        view.Y = Pos.Center();
                        break;

                    case 3:
                        view.Y = Pos.At(_yVal);
                        break;
                    }

                    switch (_wRadioGroup.SelectedItem)
                    {
                    case 0:
                        view.Width = Dim.Percent(_wVal);
                        break;

                    case 1:
                        view.Width = Dim.Fill(_wVal);
                        break;

                    case 2:
                        view.Width = Dim.Sized(_wVal);
                        break;
                    }

                    switch (_hRadioGroup.SelectedItem)
                    {
                    case 0:
                        view.Height = Dim.Percent(_hVal);
                        break;

                    case 1:
                        view.Height = Dim.Fill(_hVal);
                        break;

                    case 2:
                        view.Height = Dim.Sized(_hVal);
                        break;
                    }
                } catch (Exception e) {
                    MessageBox.ErrorQuery("Exception", e.Message, "Ok");
                } finally {
                    view.LayoutStyle = layout;
                }
                UpdateTitle(view);
            }

            void UpdateSettings(View view)
            {
                var x = view.X.ToString();
                var y = view.Y.ToString();

                _xRadioGroup.SelectedItem = posNames.IndexOf(posNames.Where(s => x.Contains(s)).First());
                _yRadioGroup.SelectedItem = posNames.IndexOf(posNames.Where(s => y.Contains(s)).First());
                _xText.Text = $"{view.Frame.X}";
                _yText.Text = $"{view.Frame.Y}";

                var w = view.Width.ToString();
                var h = view.Height.ToString();

                _wRadioGroup.SelectedItem = dimNames.IndexOf(dimNames.Where(s => w.Contains(s)).First());
                _hRadioGroup.SelectedItem = dimNames.IndexOf(dimNames.Where(s => h.Contains(s)).First());
                _wText.Text = $"{view.Frame.Width}";
                _hText.Text = $"{view.Frame.Height}";
            }

            void UpdateTitle(View view)
            {
                _hostPane.Title = $"{view.GetType ().Name} - {view.X.ToString ()}, {view.Y.ToString ()}, {view.Width.ToString ()}, {view.Height.ToString ()}";
            }

            List <Type> GetAllViewClassesCollection()
            {
                List <Type> types = new List <Type> ();

                foreach (Type type in typeof(View).Assembly.GetTypes()
                         .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf(typeof(View))))
                {
                    types.Add(type);
                }
                return(types);
            }

            void ClearClass(View view)
            {
                // Remove existing class, if any
                if (view != null)
                {
                    view.LayoutComplete -= LayoutCompleteHandler;
                    _hostPane.Remove(view);
                    view.Dispose();
                    _hostPane.Clear();
                }
            }

            View CreateClass(Type type)
            {
                // If we are to create a generic Type
                if (type.IsGenericType)
                {
                    // For each of the <T> arguments
                    List <Type> typeArguments = new List <Type> ();

                    // use <object>
                    foreach (var arg in type.GetGenericArguments())
                    {
                        typeArguments.Add(typeof(object));
                    }

                    // And change what type we are instantiating from MyClass<T> to MyClass<object>
                    type = type.MakeGenericType(typeArguments.ToArray());
                }
                // Instantiate view
                var view = (View)Activator.CreateInstance(type);

                //_curView.X = Pos.Center ();
                //_curView.Y = Pos.Center ();
                view.Width  = Dim.Percent(75);
                view.Height = Dim.Percent(75);

                // Set the colorscheme to make it stand out if is null by default
                if (view.ColorScheme == null)
                {
                    view.ColorScheme = Colors.Base;
                }

                // If the view supports a Text property, set it so we have something to look at
                if (view.GetType().GetProperty("Text") != null)
                {
                    try {
                        view.GetType().GetProperty("Text")?.GetSetMethod()?.Invoke(view, new [] { ustring.Make("Test Text") });
                    } catch (TargetInvocationException e) {
                        MessageBox.ErrorQuery("Exception", e.InnerException.Message, "Ok");
                        view = null;
                    }
                }

                // If the view supports a Title property, set it so we have something to look at
                if (view != null && view.GetType().GetProperty("Title") != null)
                {
                    view?.GetType().GetProperty("Title")?.GetSetMethod()?.Invoke(view, new [] { ustring.Make("Test Title") });
                }

                // If the view supports a Source property, set it so we have something to look at
                if (view != null && view.GetType().GetProperty("Source") != null && view.GetType().GetProperty("Source").PropertyType == typeof(Terminal.Gui.IListDataSource))
                {
                    var source = new ListWrapper(new List <ustring> ()
                    {
                        ustring.Make("Test Text #1"), ustring.Make("Test Text #2"), ustring.Make("Test Text #3")
                    });
                    view?.GetType().GetProperty("Source")?.GetSetMethod()?.Invoke(view, new [] { source });
                }

                // Set Settings
                _computedCheckBox.Checked = view.LayoutStyle == LayoutStyle.Computed;

                // Add
                _hostPane.Add(view);
                //DimPosChanged ();
                _hostPane.LayoutSubviews();
                _hostPane.Clear();
                _hostPane.SetNeedsDisplay();
                UpdateSettings(view);
                UpdateTitle(view);

                view.LayoutComplete += LayoutCompleteHandler;

                return(view);
            }

            void LayoutCompleteHandler(View.LayoutEventArgs args)
            {
                UpdateTitle(_curView);
            }
        }
Exemple #8
0
        public void LeftTopBottomRight_Win_ShouldNotThrow()
        {
            // Setup Fake driver
            (Window win, Button button) setup()
            {
                Application.Init(new FakeDriver(), new FakeMainLoop(() => FakeConsole.ReadKey(true)));
                Application.Iteration = () => {
                    Application.RequestStop();
                };
                var win = new Window("window")
                {
                    X      = 0,
                    Y      = 0,
                    Width  = Dim.Fill(),
                    Height = Dim.Fill(),
                };

                Application.Top.Add(win);

                var button = new Button("button")
                {
                    X = Pos.Center(),
                };

                win.Add(button);

                return(win, button);
            }

            Application.RunState rs;

            void cleanup(Application.RunState rs)
            {
                // Cleanup
                Application.End(rs);
            }

            // Test cases:
            var app = setup();

            app.button.Y = Pos.Left(app.win);
            rs           = Application.Begin(Application.Top);
            Application.Run();
            cleanup(rs);

            app          = setup();
            app.button.Y = Pos.X(app.win);
            rs           = Application.Begin(Application.Top);
            Application.Run();
            cleanup(rs);

            app          = setup();
            app.button.Y = Pos.Top(app.win);
            rs           = Application.Begin(Application.Top);
            Application.Run();
            cleanup(rs);

            app          = setup();
            app.button.Y = Pos.Y(app.win);
            rs           = Application.Begin(Application.Top);
            Application.Run();
            cleanup(rs);

            app          = setup();
            app.button.Y = Pos.Bottom(app.win);
            rs           = Application.Begin(Application.Top);
            Application.Run();
            cleanup(rs);

            app          = setup();
            app.button.Y = Pos.Right(app.win);
            rs           = Application.Begin(Application.Top);
            Application.Run();
            cleanup(rs);
        }
Exemple #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Toplevel"/> class with <see cref="LayoutStyle.Computed"/> layout, defaulting to full screen.
 /// </summary>
 public Toplevel() : base()
 {
     Initialize();
     Width  = Dim.Fill();
     Height = Dim.Fill();
 }
Exemple #10
0
        public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically()
        {
            Application.Init(new FakeDriver(), new NetMainLoop(() => FakeConsole.ReadKey(true)));

            var t = new Toplevel()
            {
                Id = "0",
            };

            var w = new Window()
            {
                Id = "t", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var v1 = new View()
            {
                Id = "v1", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var v2 = new View()
            {
                Id = "v2", Width = Dim.Fill(), Height = Dim.Fill()
            };

            int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;

            t.Initialized += (s, e) => {
                tc++;
                Assert.Equal(1, tc);
                Assert.Equal(0, wc);
                Assert.Equal(0, v1c);
                Assert.Equal(0, v2c);
                Assert.Equal(0, sv1c);

                Assert.True(t.CanFocus);
                Assert.True(w.CanFocus);
                Assert.False(v1.CanFocus);
                Assert.False(v2.CanFocus);

                Application.Refresh();
            };
            w.Initialized += (s, e) => {
                wc++;
                Assert.Equal(t.Frame.Width, w.Frame.Width);
                Assert.Equal(t.Frame.Height, w.Frame.Height);
            };
            v1.Initialized += (s, e) => {
                v1c++;
                Assert.Equal(t.Frame.Width, v1.Frame.Width);
                Assert.Equal(t.Frame.Height, v1.Frame.Height);
            };
            v2.Initialized += (s, e) => {
                v2c++;
                Assert.Equal(t.Frame.Width, v2.Frame.Width);
                Assert.Equal(t.Frame.Height, v2.Frame.Height);
            };
            w.Add(v1, v2);
            t.Add(w);

            Application.Iteration = () => {
                var sv1 = new View()
                {
                    Id = "sv1", Width = Dim.Fill(), Height = Dim.Fill()
                };

                sv1.Initialized += (s, e) => {
                    sv1c++;
                    Assert.NotEqual(t.Frame.Width, sv1.Frame.Width);
                    Assert.NotEqual(t.Frame.Height, sv1.Frame.Height);
                    Assert.False(sv1.CanFocus);
                    sv1.CanFocus = true;
                    Assert.True(sv1.CanFocus);
                };

                v1.Add(sv1);

                Application.Refresh();
                t.Running = false;
            };

            Application.Run(t);
            Application.Shutdown();

            Assert.Equal(1, tc);
            Assert.Equal(1, wc);
            Assert.Equal(1, v1c);
            Assert.Equal(1, v2c);
            Assert.Equal(1, sv1c);

            Assert.True(t.CanFocus);
            Assert.True(w.CanFocus);
            Assert.False(v1.CanFocus);
            Assert.False(v2.CanFocus);
        }
Exemple #11
0
        static int QueryFull(bool useErrorColors, int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
        {
            const int defaultWidth = 50;
            int       textWidth    = TextFormatter.MaxWidth(message, width == 0 ? defaultWidth : width);
            int       textHeight   = TextFormatter.MaxLines(message, textWidth); // message.Count (ustring.Make ('\n')) + 1;
            int       msgboxHeight = Math.Max(1, textHeight) + 3;                // textHeight + (top + top padding + buttons + bottom)

            // Create button array for Dialog
            int           count      = 0;
            List <Button> buttonList = new List <Button> ();

            if (buttons != null && defaultButton > buttons.Length - 1)
            {
                defaultButton = buttons.Length - 1;
            }
            foreach (var s in buttons)
            {
                var b = new Button(s);
                if (count == defaultButton)
                {
                    b.IsDefault = true;
                }
                buttonList.Add(b);
                count++;
            }

            // Create Dialog (retain backwards compat by supporting specifying height/width)
            Dialog d;

            if (width == 0 & height == 0)
            {
                d        = new Dialog(title, buttonList.ToArray());
                d.Height = msgboxHeight;
            }
            else
            {
                d = new Dialog(title, Math.Max(width, textWidth) + 4, height, buttonList.ToArray());
            }

            if (useErrorColors)
            {
                d.ColorScheme = Colors.Error;
            }

            if (message != null)
            {
                var l = new Label(textWidth > width ? 0 : (width - 4 - textWidth) / 2, 1, message);
                l.LayoutStyle   = LayoutStyle.Computed;
                l.TextAlignment = TextAlignment.Centered;
                l.X             = Pos.Center();
                l.Y             = Pos.Center();
                l.Width         = Dim.Fill(2);
                l.Height        = Dim.Fill(1);
                d.Add(l);
            }

            // Dynamically size Width
            int msgboxWidth = Math.Max(defaultWidth, Math.Max(title.RuneCount + 8, Math.Max(textWidth + 4, d.GetButtonsWidth()) + 8));                 // textWidth + (left + padding + padding + right)

            d.Width = msgboxWidth;

            // Setup actions
            int clicked = -1;

            for (int n = 0; n < buttonList.Count; n++)
            {
                int buttonId = n;
                var b        = buttonList [n];
                b.Clicked += () => {
                    clicked = buttonId;
                    Application.RequestStop();
                };
                if (b.IsDefault)
                {
                    b.SetFocus();
                }
            }

            // Run the modal; do not shutdown the mainloop driver when done
            Application.Run(d);
            return(clicked);
        }
Exemple #12
0
        public void Initialized_Event_Comparing_With_Added_Event()
        {
            Application.Init(new FakeDriver(), new NetMainLoop(() => FakeConsole.ReadKey(true)));

            var t = new Toplevel()
            {
                Id = "0",
            };

            var w = new Window()
            {
                Id = "t", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var v1 = new View()
            {
                Id = "v1", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var v2 = new View()
            {
                Id = "v2", Width = Dim.Fill(), Height = Dim.Fill()
            };
            var sv1 = new View()
            {
                Id = "sv1", Width = Dim.Fill(), Height = Dim.Fill()
            };

            int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;

            w.Added += (e) => {
                Assert.Equal(e.Frame.Width, w.Frame.Width);
                Assert.Equal(e.Frame.Height, w.Frame.Height);
            };
            v1.Added += (e) => {
                Assert.Equal(e.Frame.Width, v1.Frame.Width);
                Assert.Equal(e.Frame.Height, v1.Frame.Height);
            };
            v2.Added += (e) => {
                Assert.Equal(e.Frame.Width, v2.Frame.Width);
                Assert.Equal(e.Frame.Height, v2.Frame.Height);
            };
            sv1.Added += (e) => {
                Assert.Equal(e.Frame.Width, sv1.Frame.Width);
                Assert.Equal(e.Frame.Height, sv1.Frame.Height);
            };

            t.Initialized += (s, e) => {
                tc++;
                Assert.Equal(1, tc);
                Assert.Equal(1, wc);
                Assert.Equal(1, v1c);
                Assert.Equal(1, v2c);
                Assert.Equal(1, sv1c);

                Assert.True(t.CanFocus);
                Assert.True(w.CanFocus);
                Assert.False(v1.CanFocus);
                Assert.False(v2.CanFocus);
                Assert.False(sv1.CanFocus);

                Application.Refresh();
            };
            w.Initialized += (s, e) => {
                wc++;
                Assert.Equal(t.Frame.Width, w.Frame.Width);
                Assert.Equal(t.Frame.Height, w.Frame.Height);
            };
            v1.Initialized += (s, e) => {
                v1c++;
                Assert.Equal(t.Frame.Width, v1.Frame.Width);
                Assert.Equal(t.Frame.Height, v1.Frame.Height);
            };
            v2.Initialized += (s, e) => {
                v2c++;
                Assert.Equal(t.Frame.Width, v2.Frame.Width);
                Assert.Equal(t.Frame.Height, v2.Frame.Height);
            };
            sv1.Initialized += (s, e) => {
                sv1c++;
                Assert.Equal(t.Frame.Width, sv1.Frame.Width);
                Assert.Equal(t.Frame.Height, sv1.Frame.Height);
                Assert.False(sv1.CanFocus);
                Assert.Throws <InvalidOperationException> (() => sv1.CanFocus = true);
                Assert.False(sv1.CanFocus);
            };

            v1.Add(sv1);
            w.Add(v1, v2);
            t.Add(w);

            Application.Iteration = () => {
                Application.Refresh();
                t.Running = false;
            };

            Application.Run(t);
            Application.Shutdown();

            Assert.Equal(1, tc);
            Assert.Equal(1, wc);
            Assert.Equal(1, v1c);
            Assert.Equal(1, v2c);
            Assert.Equal(1, sv1c);

            Assert.True(t.CanFocus);
            Assert.True(w.CanFocus);
            Assert.False(v1.CanFocus);
            Assert.False(v2.CanFocus);
            Assert.False(sv1.CanFocus);

            v1.CanFocus = true;
            Assert.False(sv1.CanFocus);              // False because sv1 was disposed and it isn't a subview of v1.
        }
Exemple #13
0
        public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height()
        {
            // Testing with the Button because it properly handles the Dim class.

            Application.Init(new FakeDriver(), new FakeMainLoop(() => FakeConsole.ReadKey(true)));

            var t = Application.Top;

            var w = new Window("w")
            {
                Width  = 100,
                Height = 100
            };

            var f1 = new FrameView("f1")
            {
                X      = 0,
                Y      = 0,
                Width  = Dim.Percent(50),
                Height = 5
            };

            var f2 = new FrameView("f2")
            {
                X      = Pos.Right(f1),
                Y      = 0,
                Width  = Dim.Fill(),
                Height = 5
            };

            var v1 = new Button("v1")
            {
                X      = Pos.X(f1) + 2,
                Y      = Pos.Bottom(f1) + 2,
                Width  = Dim.Width(f1) - 2,
                Height = Dim.Fill() - 2
            };

            var v2 = new Button("v2")
            {
                X      = Pos.X(f2) + 2,
                Y      = Pos.Bottom(f2) + 2,
                Width  = Dim.Width(f2) - 2,
                Height = Dim.Fill() - 2
            };

            var v3 = new Button("v3")
            {
                Width  = Dim.Percent(10),
                Height = Dim.Percent(10)
            };

            var v4 = new Button("v4")
            {
                Width  = Dim.Sized(50),
                Height = Dim.Sized(50)
            };

            var v5 = new Button("v5")
            {
                Width  = Dim.Width(v1) - Dim.Width(v3),
                Height = Dim.Height(v1) - Dim.Height(v3)
            };

            var v6 = new Button("v6")
            {
                X      = Pos.X(f2),
                Y      = Pos.Bottom(f2) + 2,
                Width  = Dim.Percent(20, true),
                Height = Dim.Percent(20, true)
            };

            w.Add(f1, f2, v1, v2, v3, v4, v5, v6);
            t.Add(w);

            t.Ready += () => {
                Assert.Equal("Dim.Absolute(100)", w.Width.ToString());
                Assert.Equal("Dim.Absolute(100)", w.Height.ToString());
                Assert.Equal(100, w.Frame.Width);
                Assert.Equal(100, w.Frame.Height);

                Assert.Equal("Dim.Factor(factor=0.5, remaining=False)", f1.Width.ToString());
                Assert.Equal("Dim.Absolute(5)", f1.Height.ToString());
                Assert.Equal(49, f1.Frame.Width);                  // 50-1=49
                Assert.Equal(5, f1.Frame.Height);

                Assert.Equal("Dim.Fill(margin=0)", f2.Width.ToString());
                Assert.Equal("Dim.Absolute(5)", f2.Height.ToString());
                Assert.Equal(49, f2.Frame.Width);                  // 50-1=49
                Assert.Equal(5, f2.Frame.Height);

                Assert.Equal("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v1.Width.ToString());
                Assert.Equal("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v1.Height.ToString());
                Assert.Equal(47, v1.Frame.Width);                  // 49-2=47
                Assert.Equal(89, v1.Frame.Height);                 // 98-5-2-2=89


                Assert.Equal("Dim.Combine(DimView(side=Width, target=FrameView()({X=49,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v2.Width.ToString());
                Assert.Equal("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v2.Height.ToString());
                Assert.Equal(47, v2.Frame.Width);                  // 49-2=47
                Assert.Equal(89, v2.Frame.Height);                 // 98-5-2-2=89

                Assert.Equal("Dim.Factor(factor=0.1, remaining=False)", v3.Width.ToString());
                Assert.Equal("Dim.Factor(factor=0.1, remaining=False)", v3.Height.ToString());
                Assert.Equal(9, v3.Frame.Width);                  // 98*10%=9
                Assert.Equal(9, v3.Frame.Height);                 // 98*10%=9

                Assert.Equal("Dim.Absolute(50)", v4.Width.ToString());
                Assert.Equal("Dim.Absolute(50)", v4.Height.ToString());
                Assert.Equal(50, v4.Frame.Width);
                Assert.Equal(50, v4.Frame.Height);

                Assert.Equal("Dim.Combine(DimView(side=Width, target=Button()({X=2,Y=7,Width=47,Height=89}))-DimView(side=Width, target=Button()({X=0,Y=0,Width=9,Height=9})))", v5.Width.ToString());
                Assert.Equal("Dim.Combine(DimView(side=Height, target=Button()({X=2,Y=7,Width=47,Height=89}))-DimView(side=Height, target=Button()({X=0,Y=0,Width=9,Height=9})))", v5.Height.ToString());
                Assert.Equal(38, v5.Frame.Width);                  // 47-9=38
                Assert.Equal(80, v5.Frame.Height);                 // 89-9=80

                Assert.Equal("Dim.Factor(factor=0.2, remaining=True)", v6.Width.ToString());
                Assert.Equal("Dim.Factor(factor=0.2, remaining=True)", v6.Height.ToString());
                Assert.Equal(9, v6.Frame.Width);                  // 47*20%=9
                Assert.Equal(18, v6.Frame.Height);                // 89*20%=18


                w.Width  = 200;
                w.Height = 200;
                t.LayoutSubviews();

                Assert.Equal("Dim.Absolute(200)", w.Width.ToString());
                Assert.Equal("Dim.Absolute(200)", w.Height.ToString());
                Assert.Equal(200, w.Frame.Width);
                Assert.Equal(200, w.Frame.Height);

                f1.Text = "Frame1";
                Assert.Equal("Dim.Factor(factor=0.5, remaining=False)", f1.Width.ToString());
                Assert.Equal("Dim.Absolute(5)", f1.Height.ToString());
                Assert.Equal(99, f1.Frame.Width);                  // 100-1=99
                Assert.Equal(5, f1.Frame.Height);

                f2.Text = "Frame2";
                Assert.Equal("Dim.Fill(margin=0)", f2.Width.ToString());
                Assert.Equal("Dim.Absolute(5)", f2.Height.ToString());
                Assert.Equal(99, f2.Frame.Width);                  // 100-1=99
                Assert.Equal(5, f2.Frame.Height);

                v1.Text = "Button1";
                Assert.Equal("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v1.Width.ToString());
                Assert.Equal("Dim.Absolute(1)", v1.Height.ToString());
                Assert.Equal(97, v1.Frame.Width);                  // 99-2=97
                Assert.Equal(1, v1.Frame.Height);                  // 1 because is Dim.DimAbsolute

                v2.Text = "Button2";
                Assert.Equal("Dim.Combine(DimView(side=Width, target=FrameView()({X=99,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v2.Width.ToString());
                Assert.Equal("Dim.Absolute(1)", v2.Height.ToString());
                Assert.Equal(97, v2.Frame.Width);                  // 99-2=97
                Assert.Equal(1, v2.Frame.Height);                  // 1 because is Dim.DimAbsolute

                v3.Text = "Button3";
                Assert.Equal("Dim.Factor(factor=0.1, remaining=False)", v3.Width.ToString());
                Assert.Equal("Dim.Absolute(1)", v3.Height.ToString());
                Assert.Equal(19, v3.Frame.Width);                  // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
                Assert.Equal(1, v3.Frame.Height);                  // 1 because is Dim.DimAbsolute

                v4.Text = "Button4";
                Assert.Equal("Dim.Absolute(11)", v4.Width.ToString());
                Assert.Equal("Dim.Absolute(1)", v4.Height.ToString());
                Assert.Equal(11, v4.Frame.Width);                  // 11 is the text length and because is Dim.DimAbsolute
                Assert.Equal(1, v4.Frame.Height);                  // 1 because is Dim.DimAbsolute

                v5.Text = "Button5";
                Assert.Equal("Dim.Combine(DimView(side=Width, target=Button()({X=2,Y=7,Width=97,Height=1}))-DimView(side=Width, target=Button()({X=0,Y=0,Width=19,Height=1})))", v5.Width.ToString());
                Assert.Equal("Dim.Absolute(1)", v5.Height.ToString());
                Assert.Equal(78, v5.Frame.Width);                  // 97-19=78
                Assert.Equal(1, v5.Frame.Height);                  // 1 because is Dim.DimAbsolute

                v6.Text = "Button6";
                Assert.Equal("Dim.Factor(factor=0.2, remaining=True)", v6.Width.ToString());
                Assert.Equal("Dim.Absolute(1)", v6.Height.ToString());
                Assert.Equal(19, v6.Frame.Width);                  // 99*20%=19
                Assert.Equal(1, v6.Frame.Height);                  // 1 because is Dim.DimAbsolute
            };

            Application.Iteration += () => Application.RequestStop();

            Application.Run();
            Application.Shutdown();
        }
Exemple #14
0
        void Initialize(Rect frame)
        {
            contentView = new View(frame);
            vertical    = new ScrollBarView(1, 0, isVertical: true)
            {
                X      = Pos.AnchorEnd(1),
                Y      = 0,
                Width  = 1,
                Height = Dim.Fill(showHorizontalScrollIndicator ? 1 : 0)
            };
            vertical.ChangedPosition += delegate {
                ContentOffset = new Point(ContentOffset.X, vertical.Position);
            };
            vertical.Host = this;
            horizontal    = new ScrollBarView(1, 0, isVertical: false)
            {
                X      = 0,
                Y      = Pos.AnchorEnd(1),
                Width  = Dim.Fill(showVerticalScrollIndicator ? 1 : 0),
                Height = 1
            };
            horizontal.ChangedPosition += delegate {
                ContentOffset = new Point(horizontal.Position, ContentOffset.Y);
            };
            horizontal.Host               = this;
            vertical.OtherScrollBarView   = horizontal;
            horizontal.OtherScrollBarView = vertical;
            base.Add(contentView);
            CanFocus = true;

            MouseEnter             += View_MouseEnter;
            MouseLeave             += View_MouseLeave;
            contentView.MouseEnter += View_MouseEnter;
            contentView.MouseLeave += View_MouseLeave;

            // Things this view knows how to do
            AddCommand(Command.ScrollUp, () => ScrollUp(1));
            AddCommand(Command.ScrollDown, () => ScrollDown(1));
            AddCommand(Command.ScrollLeft, () => ScrollLeft(1));
            AddCommand(Command.ScrollRight, () => ScrollRight(1));
            AddCommand(Command.PageUp, () => ScrollUp(Bounds.Height));
            AddCommand(Command.PageDown, () => ScrollDown(Bounds.Height));
            AddCommand(Command.PageLeft, () => ScrollLeft(Bounds.Width));
            AddCommand(Command.PageRight, () => ScrollRight(Bounds.Width));
            AddCommand(Command.TopHome, () => ScrollUp(contentSize.Height));
            AddCommand(Command.BottomEnd, () => ScrollDown(contentSize.Height));
            AddCommand(Command.LeftHome, () => ScrollLeft(contentSize.Width));
            AddCommand(Command.RightEnd, () => ScrollRight(contentSize.Width));

            // Default keybindings for this view
            AddKeyBinding(Key.CursorUp, Command.ScrollUp);
            AddKeyBinding(Key.CursorDown, Command.ScrollDown);
            AddKeyBinding(Key.CursorLeft, Command.ScrollLeft);
            AddKeyBinding(Key.CursorRight, Command.ScrollRight);

            AddKeyBinding(Key.PageUp, Command.PageUp);
            AddKeyBinding((Key)'v' | Key.AltMask, Command.PageUp);

            AddKeyBinding(Key.PageDown, Command.PageDown);
            AddKeyBinding(Key.V | Key.CtrlMask, Command.PageDown);

            AddKeyBinding(Key.PageUp | Key.CtrlMask, Command.PageLeft);
            AddKeyBinding(Key.PageDown | Key.CtrlMask, Command.PageRight);
            AddKeyBinding(Key.Home, Command.TopHome);
            AddKeyBinding(Key.End, Command.BottomEnd);
            AddKeyBinding(Key.Home | Key.CtrlMask, Command.LeftHome);
            AddKeyBinding(Key.End | Key.CtrlMask, Command.RightEnd);
        }