From: Kevin Lamonte Date: Sun, 15 Mar 2015 01:21:02 +0000 (-0400) Subject: radio buttons X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=00d2622b0fff2411dc81ee2a0cc43a5f3f52564c;p=fanfix.git radio buttons --- diff --git a/README.md b/README.md index 0ee5726..f36e850 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,6 @@ Many tasks remain before calling this version 1.0: - TDirectoryList - TField - TMessageBox -- TRadioGroup / TRadioButton - THScroller / TVScroller - TText - TTreeView diff --git a/demos/Demo1.java b/demos/Demo1.java index db32708..f8e8b63 100644 --- a/demos/Demo1.java +++ b/demos/Demo1.java @@ -60,19 +60,18 @@ class DemoCheckboxWindow extends TWindow { addCheckbox(35, row++, "Checkbox 2", true); row += 2; - /* - auto group = addRadioGroup(1, row, "Group 1"); + TRadioGroup group = addRadioGroup(1, row, "Group 1"); group.addRadioButton("Radio option 1"); group.addRadioButton("Radio option 2"); group.addRadioButton("Radio option 3"); - addButton("&Close Window", (width - 14) / 2, height - 4, - { - application.closeWindow(this); + addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4, + new TAction() { + public void DO() { + DemoCheckboxWindow.this.getApplication().closeWindow(DemoCheckboxWindow.this); + } } - ); - */ } } diff --git a/src/jexer/TCheckbox.java b/src/jexer/TCheckbox.java index 556a11b..939f3fe 100644 --- a/src/jexer/TCheckbox.java +++ b/src/jexer/TCheckbox.java @@ -84,8 +84,7 @@ public final class TCheckbox extends TWidget { * @return true if the mouse is currently on the checkbox */ private boolean mouseOnCheckbox(final TMouseEvent mouse) { - if ((mouse != null) - && (mouse.getY() == 0) + if ((mouse.getY() == 0) && (mouse.getX() >= 0) && (mouse.getX() <= 2) ) { diff --git a/src/jexer/TRadioButton.java b/src/jexer/TRadioButton.java new file mode 100644 index 0000000..23663bb --- /dev/null +++ b/src/jexer/TRadioButton.java @@ -0,0 +1,193 @@ +/** + * Jexer - Java Text User Interface + * + * License: LGPLv3 or later + * + * This module is licensed under the GNU Lesser General Public License + * Version 3. Please see the file "COPYING" in this directory for more + * information about the GNU Lesser General Public License Version 3. + * + * Copyright (C) 2015 Kevin Lamonte + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see + * http://www.gnu.org/licenses/, or write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + * @author Kevin Lamonte [kevin.lamonte@gmail.com] + * @version 1 + */ +package jexer; + +import jexer.bits.CellAttributes; +import jexer.bits.GraphicsChars; +import jexer.event.TKeypressEvent; +import jexer.event.TMouseEvent; +import static jexer.TKeypress.*; + +/** + * TRadioButton implements a selectable radio button. + */ +public final class TRadioButton extends TWidget { + + /** + * RadioButton state, true means selected. + */ + private boolean selected = false; + + /** + * Get RadioButton state, true means selected. + * + * @return if true then this is the one button in the group that is + * selected + */ + public boolean getSelected() { + return selected; + } + + /** + * Set RadioButton state, true means selected. Note package private + * access. + * + * @param selected if true then this is the one button in the group that + * is selected + */ + void setSelected(final boolean selected) { + this.selected = selected; + } + + /** + * Label for this radio button. + */ + private String label; + + /** + * ID for this radio button. Buttons start counting at 1 in the + * RadioGroup. + */ + private int id; + + /** + * Get ID for this radio button. Buttons start counting at 1 in the + * RadioGroup. + * + * @return the ID + */ + public int getId() { + return id; + } + + /** + * Public constructor. + * + * @param parent parent widget + * @param x column relative to parent + * @param y row relative to parent + * @param label label to display next to (right of) the radiobutton + * @param id ID for this radio button + */ + public TRadioButton(final TRadioGroup parent, final int x, final int y, + final String label, final int id) { + + // Set parent and window + super(parent); + + setX(x); + setY(y); + setHeight(1); + this.label = label; + setWidth(label.length() + 4); + this.id = id; + + setHasCursor(true); + setCursorX(1); + } + + /** + * Returns true if the mouse is currently on the radio button. + * + * @param mouse mouse event + * @return if true the mouse is currently on the radio button + */ + private boolean mouseOnRadioButton(final TMouseEvent mouse) { + if ((mouse.getY() == 0) + && (mouse.getX() >= 0) + && (mouse.getX() <= 2) + ) { + return true; + } + return false; + } + + /** + * Draw a radio button with label. + */ + @Override + public void draw() { + CellAttributes radioButtonColor; + + if (getAbsoluteActive()) { + radioButtonColor = getTheme().getColor("tradiobutton.active"); + } else { + radioButtonColor = getTheme().getColor("tradiobutton.inactive"); + } + + getScreen().putCharXY(0, 0, '(', radioButtonColor); + if (selected) { + getScreen().putCharXY(1, 0, GraphicsChars.CP437[0x07], + radioButtonColor); + } else { + getScreen().putCharXY(1, 0, ' ', radioButtonColor); + } + getScreen().putCharXY(2, 0, ')', radioButtonColor); + getScreen().putStrXY(4, 0, label, radioButtonColor); + } + + /** + * Handle mouse button presses. + * + * @param mouse mouse button press event + */ + @Override + public void onMouseDown(final TMouseEvent mouse) { + if ((mouseOnRadioButton(mouse)) && (mouse.getMouse1())) { + // Switch state + selected = !selected; + if (selected) { + ((TRadioGroup) getParent()).setSelected(this); + } + } + } + + /** + * Handle keystrokes. + * + * @param keypress keystroke event + */ + @Override + public void onKeypress(final TKeypressEvent keypress) { + + if (keypress.equals(kbSpace)) { + selected = !selected; + if (selected) { + ((TRadioGroup) getParent()).setSelected(this); + } + return; + } + + // Pass to parent for the things we don't care about. + super.onKeypress(keypress); + } + +} diff --git a/src/jexer/TRadioGroup.java b/src/jexer/TRadioGroup.java new file mode 100644 index 0000000..c1b6523 --- /dev/null +++ b/src/jexer/TRadioGroup.java @@ -0,0 +1,133 @@ +/** + * Jexer - Java Text User Interface + * + * License: LGPLv3 or later + * + * This module is licensed under the GNU Lesser General Public License + * Version 3. Please see the file "COPYING" in this directory for more + * information about the GNU Lesser General Public License Version 3. + * + * Copyright (C) 2015 Kevin Lamonte + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see + * http://www.gnu.org/licenses/, or write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + * @author Kevin Lamonte [kevin.lamonte@gmail.com] + * @version 1 + */ +package jexer; + +import jexer.bits.CellAttributes; + +/** + * TRadioGroup is a collection of TRadioButtons with a box and label. + */ +public final class TRadioGroup extends TWidget { + + /** + * Label for this radio button group. + */ + private String label; + + /** + * Only one of my children can be selected. + */ + private TRadioButton selectedButton = null; + + /** + * Get the radio button ID that was selected. + * + * @return ID of the selected button, or 0 if no button is selected + */ + public int getSelected() { + if (selectedButton == null) { + return 0; + } + return selectedButton.getId(); + } + + /** + * Set the new selected radio button. Note package private access. + * + * @param button new button that became selected + */ + void setSelected(final TRadioButton button) { + assert (button.getSelected()); + if (selectedButton != null) { + selectedButton.setSelected(false); + } + selectedButton = button; + } + + /** + * Public constructor. + * + * @param parent parent widget + * @param x column relative to parent + * @param y row relative to parent + * @param label label to display on the group box + */ + public TRadioGroup(final TWidget parent, final int x, final int y, + final String label) { + + // Set parent and window + super(parent); + + setX(x); + setY(y); + setHeight(2); + this.label = label; + setWidth(label.length() + 4); + } + + /** + * Draw a radio button with label. + */ + @Override + public void draw() { + CellAttributes radioGroupColor; + + if (getAbsoluteActive()) { + radioGroupColor = getTheme().getColor("tradiogroup.active"); + } else { + radioGroupColor = getTheme().getColor("tradiogroup.inactive"); + } + + getScreen().drawBox(0, 0, getWidth(), getHeight(), + radioGroupColor, radioGroupColor, 3, false); + + getScreen().hLineXY(1, 0, label.length() + 2, ' ', radioGroupColor); + getScreen().putStrXY(2, 0, label, radioGroupColor); + } + + /** + * Convenience function to add a radio button to this group. + * + * @param label label to display next to (right of) the radiobutton + * @return the new radio button + */ + public TRadioButton addRadioButton(final String label) { + int buttonX = 1; + int buttonY = getChildren().size() + 1; + if (label.length() + 4 > getWidth()) { + setWidth(label.length() + 7); + } + setHeight(getChildren().size() + 3); + return new TRadioButton(this, buttonX, buttonY, label, + getChildren().size() + 1); + } + +} diff --git a/src/jexer/TWidget.java b/src/jexer/TWidget.java index 1ab54e2..0f920ba 100644 --- a/src/jexer/TWidget.java +++ b/src/jexer/TWidget.java @@ -1023,6 +1023,7 @@ public abstract class TWidget implements Comparable { * @param y row relative to parent * @param width width of progress bar * @param value initial value of percent complete + * @return the new progress bar */ public final TProgressBar addProgressBar(final int x, final int y, final int width, final int value) { @@ -1030,4 +1031,20 @@ public abstract class TWidget implements Comparable { return new TProgressBar(this, x, y, width, value); } + /** + * Convenience function to add a radio button group to this + * container/window. + * + * @param x column relative to parent + * @param y row relative to parent + * @param label label to display on the group box + * @return the new radio button group + */ + public final TRadioGroup addRadioGroup(final int x, final int y, + final String label) { + + return new TRadioGroup(this, x, y, label); + } + + }