X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTStatusBar.java;h=fbd79da850c320f0fa3a7a54696d7da4ed31b857;hb=HEAD;hp=d03d5262a82035077b845d6cc3df7a879df00a5f;hpb=2ce6dab2bbd951e6d0f09f94759efda5ee4b65ac;p=fanfix.git diff --git a/src/jexer/TStatusBar.java b/src/jexer/TStatusBar.java index d03d526..fbd79da 100644 --- a/src/jexer/TStatusBar.java +++ b/src/jexer/TStatusBar.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2016 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -29,9 +29,11 @@ package jexer; import java.util.ArrayList; +import java.util.List; import jexer.bits.CellAttributes; import jexer.bits.GraphicsChars; +import jexer.bits.StringUtils; import jexer.event.TCommandEvent; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; @@ -39,7 +41,26 @@ import jexer.event.TMouseEvent; /** * TStatusBar implements a status line with clickable buttons. */ -public final class TStatusBar extends TWidget { +public class TStatusBar extends TWidget { + + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Remember mouse state. + */ + private TMouseEvent mouse; + + /** + * The text to display on the right side of the shortcut keys. + */ + private String text = null; + + /** + * The shortcut keys. + */ + private List keys = new ArrayList(); /** * A single shortcut key. @@ -77,7 +98,8 @@ public final class TStatusBar extends TWidget { * @return the number of columns this takes when drawn */ public int width() { - return this.label.length() + this.key.toString().length() + 3; + return StringUtils.width(this.label) + + StringUtils.width(this.key.toString()) + 3; } /** @@ -98,122 +120,38 @@ public final class TStatusBar extends TWidget { } - /** - * Remember mouse state. - */ - private TMouseEvent mouse; - - /** - * The text to display on the right side of the shortcut keys. - */ - private String text = null; - - /** - * The shortcut keys. - */ - private ArrayList keys = new ArrayList(); - - /** - * Add a key to this status bar. - * - * @param key the key to trigger on - * @param cmd the command event to issue when key is pressed or this item - * is clicked - * @param label the label for this action - */ - public void addShortcutKeypress(final TKeypress key, final TCommand cmd, - final String label) { - - TStatusBarKey newKey = new TStatusBarKey(key, cmd, label); - if (keys.size() > 0) { - TStatusBarKey oldKey = keys.get(keys.size() - 1); - newKey.x = oldKey.x + oldKey.width(); - } - keys.add(newKey); - } - - /** - * Set the text to display on the right side of the shortcut keys. - * - * @param text the new text - */ - public void setText(final String text) { - this.text = text; - } + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Public constructor. * - * @param parent parent widget + * @param window the window associated with this status bar * @param text text for the bar on the bottom row */ - public TStatusBar(final TWidget parent, final String text) { + public TStatusBar(final TWindow window, final String text) { - // Set parent and window - super(parent, false, 0, 0, text.length(), 1); + // TStatusBar is a parentless widget, because TApplication handles + // its drawing and event routing directly. + super(null, false, 0, 0, StringUtils.width(text), 1); this.text = text; + setWindow(window); } /** * Public constructor. * - * @param parent parent widget + * @param window the window associated with this status bar */ - public TStatusBar(final TWidget parent) { - this(parent, ""); + public TStatusBar(final TWindow window) { + this(window, ""); } - /** - * Draw the bar. - */ - @Override - public void draw() { - CellAttributes barColor = new CellAttributes(); - barColor.setTo(getTheme().getColor("tstatusbar.text")); - CellAttributes keyColor = new CellAttributes(); - keyColor.setTo(getTheme().getColor("tstatusbar.button")); - CellAttributes selectedColor = new CellAttributes(); - selectedColor.setTo(getTheme().getColor("tstatusbar.selected")); - - // Status bar is weird. Its draw() method is called directly by - // TApplication after everything is drawn, and after - // Screen.resetClipping(). So at this point we are drawing in - // absolute coordinates, not relative to our TWindow. - int row = getScreen().getHeight() - 1; - int width = getScreen().getWidth(); - - getScreen().hLineXY(0, row, width, ' ', barColor); - - int col = 0; - for (TStatusBarKey key: keys) { - String keyStr = key.key.toString(); - if (key.selected) { - getScreen().putCharXY(col++, row, ' ', selectedColor); - getScreen().putStringXY(col, row, keyStr, selectedColor); - col += keyStr.length(); - getScreen().putCharXY(col++, row, ' ', selectedColor); - getScreen().putStringXY(col, row, key.label, selectedColor); - col += key.label.length(); - getScreen().putCharXY(col++, row, ' ', selectedColor); - } else { - getScreen().putCharXY(col++, row, ' ', barColor); - getScreen().putStringXY(col, row, keyStr, keyColor); - col += keyStr.length() + 1; - getScreen().putStringXY(col, row, key.label, barColor); - col += key.label.length(); - getScreen().putCharXY(col++, row, ' ', barColor); - } - } - if (text.length() > 0) { - if (keys.size() > 0) { - getScreen().putCharXY(col++, row, GraphicsChars.VERTICAL_BAR, - barColor); - } - getScreen().putCharXY(col++, row, ' ', barColor); - getScreen().putStringXY(col, row, text, barColor); - } - } + // ------------------------------------------------------------------------ + // Event handlers --------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Handle keypresses. @@ -302,4 +240,90 @@ public final class TStatusBar extends TWidget { } } + // ------------------------------------------------------------------------ + // TWidget ---------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Draw the bar. + */ + @Override + public void draw() { + CellAttributes barColor = new CellAttributes(); + barColor.setTo(getTheme().getColor("tstatusbar.text")); + CellAttributes keyColor = new CellAttributes(); + keyColor.setTo(getTheme().getColor("tstatusbar.button")); + CellAttributes selectedColor = new CellAttributes(); + selectedColor.setTo(getTheme().getColor("tstatusbar.selected")); + + // Status bar is weird. Its draw() method is called directly by + // TApplication after everything is drawn, and after + // Screen.resetClipping(). So at this point we are drawing in + // absolute coordinates, not relative to our TWindow. + int row = getScreen().getHeight() - 1; + int width = getScreen().getWidth(); + + hLineXY(0, row, width, ' ', barColor); + + int col = 0; + for (TStatusBarKey key: keys) { + String keyStr = key.key.toString(); + if (key.selected) { + putCharXY(col++, row, ' ', selectedColor); + putStringXY(col, row, keyStr, selectedColor); + col += StringUtils.width(keyStr); + putCharXY(col++, row, ' ', selectedColor); + putStringXY(col, row, key.label, selectedColor); + col += StringUtils.width(key.label); + putCharXY(col++, row, ' ', selectedColor); + } else { + putCharXY(col++, row, ' ', barColor); + putStringXY(col, row, keyStr, keyColor); + col += StringUtils.width(keyStr) + 1; + putStringXY(col, row, key.label, barColor); + col += StringUtils.width(key.label); + putCharXY(col++, row, ' ', barColor); + } + } + if (text.length() > 0) { + if (keys.size() > 0) { + putCharXY(col++, row, GraphicsChars.VERTICAL_BAR, barColor); + } + putCharXY(col++, row, ' ', barColor); + putStringXY(col, row, text, barColor); + } + } + + // ------------------------------------------------------------------------ + // TStatusBar ------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Add a key to this status bar. + * + * @param key the key to trigger on + * @param cmd the command event to issue when key is pressed or this item + * is clicked + * @param label the label for this action + */ + public void addShortcutKeypress(final TKeypress key, final TCommand cmd, + final String label) { + + TStatusBarKey newKey = new TStatusBarKey(key, cmd, label); + if (keys.size() > 0) { + TStatusBarKey oldKey = keys.get(keys.size() - 1); + newKey.x = oldKey.x + oldKey.width(); + } + keys.add(newKey); + } + + /** + * Set the text to display on the right side of the shortcut keys. + * + * @param text the new text + */ + public void setText(final String text) { + this.text = text; + } + }