/// <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, () => ConsoleTheme.Current.PopupBg, () => ConsoleTheme.Current.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; } }
/// <returns> /// Y coordinate of bottom edge of dialog /// </returns> protected int GetBottom() { return(Formatting.ConvertCoord(bottom, Console.WindowHeight)); }
/// <returns> /// X coordinate of right edge of dialog /// </returns> protected int GetRight() { return(Formatting.ConvertCoord(right, Console.WindowWidth)); }
/// <returns> /// Y coordinate of top edge of dialog /// </returns> protected int GetTop() { return(Formatting.ConvertCoord(top, Console.WindowHeight)); }
/// <returns> /// X coordinate of left edge of dialog /// </returns> protected int GetLeft() { return(Formatting.ConvertCoord(left, Console.WindowWidth)); }
private void Draw(ConsoleTheme theme, int right, int top) { if (options.Count > 0) { right = Formatting.ConvertCoord(right, Console.WindowWidth); top = Formatting.ConvertCoord(top, Console.WindowHeight); Console.CursorVisible = false; // Space, vertical line, space, options, space, vertical line, space int w = longestLength + 6; // Horizontal lines before and after the options int h = options.Count + 2; Console.BackgroundColor = theme.MenuBg; Console.ForegroundColor = theme.MenuFg; string fullHorizLine = new string(Symbols.horizLine, longestLength + 2); for (int index = -1, y = top; y < top + h; ++index, ++y) { Console.SetCursorPosition(right - w + 1, y); // Left padding Console.Write(" "); if (index < 0) { // Draw top line Console.Write(Symbols.upperLeftCorner + fullHorizLine + Symbols.upperRightCorner); } else if (index >= options.Count) { // Draw bottom line Console.Write(Symbols.lowerLeftCorner + fullHorizLine + Symbols.lowerRightCorner); } else { ConsoleMenuOption opt = options[index]; if (opt == null) { // Draw separator Console.Write(Symbols.leftTee + fullHorizLine + Symbols.rightTee); } else { // Draw menu option Console.Write(Symbols.vertLine); if (!opt.Enabled) { Console.ForegroundColor = theme.MenuDisabledFg; } if (index == selectedOption) { // Draw highlighted menu option Console.BackgroundColor = theme.MenuSelectedBg; Console.Write(" " + AnnotatedCaption(opt) + " "); Console.BackgroundColor = theme.MenuBg; } else { // Draw normal menu option Console.Write(" " + AnnotatedCaption(opt) + " "); } if (!opt.Enabled) { Console.ForegroundColor = theme.MenuFg; } Console.Write(Symbols.vertLine); } } // Right padding Console.Write(" "); } ConsoleDialog.DrawShadow(theme, right - w + 1, top, right, top + h - 1); DrawFooter(theme); Console.SetCursorPosition(right - longestLength - 3, top + selectedOption + 1); Console.CursorVisible = true; } }