/// <summary>
        /// Add a line to the text box
        /// </summary>
        /// <param name="line">String to add</param>
        public void AddLine(string line)
        {
            lines.Add(line);
            int w = GetRight() - GetLeft() + 1 + (needScroll ? -1 : 0);

            // AddRange isn't thread-safe, it temporarily pads with nulls
            foreach (string subLine in Formatting.WordWrap(line, w))
            {
                displayLines.Add(subLine);
            }
            if (!needScroll)
            {
                int h = GetBottom() - GetTop() + 1;
                if (displayLines.Count > h)
                {
                    // We just crossed over from non-scrollbar to scrollbar,
                    // re-wrap the whole display including this line
                    needScroll = true;
                    rewrapLines();
                }
            }
            if (scrollToBottom)
            {
                ScrollToBottom();
            }
            else
            {
                // No auto-scrolling
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initialize the Dialog
        /// </summary>
        /// <param name="m">Message to show</param>
        /// <param name="hdr">Text for column header of list box</param>
        /// <param name="c">List of objects to put in the list box</param>
        /// <param name="renderer">Function to generate text for each option</param>
        /// <param name="comparer">Optional function to sort the rows</param>
        public ConsoleChoiceDialog(string m, string hdr, List <ChoiceT> c, Func <ChoiceT, string> renderer, Comparison <ChoiceT> comparer = null)
            : base()
        {
            int l = GetLeft(),
                                                 r = GetRight();
            int w = r - l + 1;

            // Resize the window to fit the content
            List <string> msgLines = Formatting.WordWrap(m, w - 4);

            int h = 2 + msgLines.Count + 1 + 1 + c.Count + 2;
            int t = (Console.WindowHeight - h) / 2;
            int b = t + h - 1;

            SetDimensions(l, t, r, b);

            // Wrapped message at top
            ConsoleTextBox tb = new ConsoleTextBox(
                l + 2, t + 2, r - 2, t + 2 + msgLines.Count - 1,
                false,
                TextAlign.Left,
                () => ConsoleTheme.Current.PopupBg,
                () => ConsoleTheme.Current.PopupFg
                );

            AddObject(tb);
            tb.AddLine(m);

            // ConsoleListBox<ChoiceT> of choices at bottom
            choices = new ConsoleListBox <ChoiceT>(
                l + 2, t + 2 + msgLines.Count + 1, r - 2, b - 2,
                c,
                new List <ConsoleListBoxColumn <ChoiceT> >()
            {
                new ConsoleListBoxColumn <ChoiceT>()
                {
                    Header   = hdr,
                    Width    = w - 6,
                    Renderer = renderer,
                    Comparer = comparer
                }
            },
                0, 0, ListSortDirection.Ascending
                );

            choices.AddTip("Enter", "Accept");
            choices.AddBinding(Keys.Enter, (object sender) => {
                return(false);
            });

            choices.AddTip("Esc", "Cancel");
            choices.AddBinding(Keys.Escape, (object sender) => {
                cancelled = true;
                return(false);
            });
            AddObject(choices);
        }
Beispiel #3
0
 /// <summary>
 /// Add a line to the text box
 /// </summary>
 /// <param name="line">String to add</param>
 public void AddLine(string line)
 {
     lines.AddRange(Formatting.WordWrap(line, GetRight() - GetLeft() + 1));
     if (scrollToBottom)
     {
         ScrollToBottom();
     }
     else
     {
         // No auto-scrolling
     }
 }
Beispiel #4
0
 /// <summary>
 /// Add a line to the text box
 /// </summary>
 /// <param name="line">String to add</param>
 public void AddLine(string line)
 {
     // AddRange isn't thread-safe, it temporarily pads with nulls
     foreach (string subLine in Formatting.WordWrap(line, GetRight() - GetLeft() + 1))
     {
         lines.Add(subLine);
     }
     if (scrollToBottom)
     {
         ScrollToBottom();
     }
     else
     {
         // No auto-scrolling
     }
 }
        private void rewrapLines()
        {
            int w = GetRight() - GetLeft() + 1 + (needScroll ? -1 : 0);

            prevTextW = w;
            int   h          = GetBottom() - GetTop() + 1;
            float scrollFrac = displayLines.Count > h
                ? (float)topLine / ((float)displayLines.Count - h)
                : 0;

            displayLines.Clear();
            foreach (string line in lines)
            {
                foreach (string subLine in Formatting.WordWrap(line, w))
                {
                    displayLines.Add(subLine);
                }
            }
            topLine = (int)Math.Max(0,
                                    scrollFrac * (displayLines.Count - h));
        }
Beispiel #6
0
        /// <summary>
        /// Initialize a dialog
        /// </summary>
        /// <param name="m">Message to show</param>
        /// <param name="btns">List of captions for buttons</param>
        /// <param name="hdr">Function to generate the header</param>
        /// <param name="ta">Alignment of the contents</param>
        /// <param name="vertOffset">Pass non-zero to move popup vertically</param>
        public ConsoleMessageDialog(string m, List <string> btns, Func <string> hdr = null, TextAlign ta = TextAlign.Center, int vertOffset = 0)
            : base()
        {
            int maxLen = Formatting.MaxLineLength(m);
            int w      = Math.Max(minWidth, Math.Min(maxLen + 6, Console.WindowWidth - 4));
            int l      = (Console.WindowWidth - w) / 2;
            int r      = -l;

            if (hdr != null)
            {
                CenterHeader = hdr;
            }

            int btnW = btns.Count * buttonWidth + (btns.Count - 1) * buttonPadding;

            if (w < btnW + 4)
            {
                // Widen the window to fit the buttons
                // Buttons will NOT wrap - use ConsoleChoiceDialog
                // if you have many large options.
                w = btnW + 4;
                l = (Console.WindowWidth - w) / 2;
                r = Console.WindowWidth - l;
            }

            List <string> messageLines = Formatting.WordWrap(m, w - 4);
            int           h            = 2 + messageLines.Count + (btns.Count > 0 ? 2 : 0) + 2;

            if (h > Console.WindowHeight - 4)
            {
                h = Console.WindowHeight - 4;
            }

            // Calculate vertical position including offset
            int t, b;

            if (vertOffset <= 0)
            {
                t = (Console.WindowHeight - h) / 2 + vertOffset;
                if (t < 1)
                {
                    t = 2;
                }
                b = t + h - 1;
            }
            else
            {
                b = (Console.WindowHeight - h) / 2 + h - 1;
                if (b >= Console.WindowHeight - 1)
                {
                    b = Console.WindowHeight - 1;
                }
                t = b - h + 1;
            }

            SetDimensions(l, t, r, b);
            int btnRow = GetBottom() - 2;

            ConsoleTextBox tb = new ConsoleTextBox(
                GetLeft() + 2, GetTop() + 2, GetRight() - 2, GetBottom() - 2 - (btns.Count > 0 ? 2 : 0),
                false,
                ta,
                th => th.PopupBg,
                th => th.PopupFg
                );

            AddObject(tb);
            tb.AddLine(m);

            int boxH = GetBottom() - 2 - (btns.Count > 0 ? 2 : 0) - (GetTop() + 2) + 1;

            if (messageLines.Count > boxH)
            {
                // Scroll
                AddTip("Cursor keys", "Scroll");
                tb.AddScrollBindings(this);
            }

            int btnLeft = (Console.WindowWidth - btnW) / 2;

            for (int i = 0; i < btns.Count; ++i)
            {
                string cap = btns[i];
                int    j   = i;
                AddObject(new ConsoleButton(btnLeft, btnRow, btnLeft + buttonWidth - 1, cap, () => {
                    selectedButton = j;
                    Quit();
                }));
                btnLeft += buttonWidth + buttonPadding;
            }
        }