Add a toggle button class.

A toggle button is a button which has two states up and down. For
example a radio button or a checkbox is a toggle button.
This commit is contained in:
Mark de Wever 2008-05-09 16:16:44 +00:00
parent 8146f5b544
commit 4ccb5dd736
10 changed files with 427 additions and 8 deletions

View file

@ -0,0 +1,136 @@
###
### Definition of the default toggle button.
### Since for this class 'default' is a bit hard we now use the checkbox as default.
###
#define TEXT
[text]
x = 25
y = TEXT_V_CENTRE}
w = "(width - 23)"
h = "(text_height)"
font_size = {FONT_SIZE_NORMAL__NORMAL}
colour = {FONT_COLOUR_ENABLED}
text = "(text)"
[/text]
#enddef
[toggle_button_definition]
id = "default"
description = "Default button"
[resolution]
min_width = 30
min_height = 22
default_width = 30
default_height = 22
max_width = 0
max_height = 22
text_extra_width = 25
text_font_size = {FONT_SIZE_NORMAL__NORMAL}
[state_enabled_up]
full_redraw = "true"
[draw]
[image]
name = "buttons/checkbox.png"
[/image]
{TEXT}
[/draw]
[/state_enabled_up]
[state_disabled_up]
full_redraw = "true"
[draw]
[image]
name = "buttons/checkbox.png"
[/image]
{TEXT}
[/draw]
[/state_disabled_up]
[state_focussed_up]
full_redraw = "true"
[draw]
[image]
name = "buttons/checkbox-active.png"
[/image]
{TEXT}
[/draw]
[/state_focussed_up]
###
### Down
###
[state_enabled_down]
full_redraw = "true"
[draw]
[image]
name = "buttons/checkbox-pressed.png"
[/image]
{TEXT}
[/draw]
[/state_enabled_down]
[state_disabled_down]
full_redraw = "true"
[draw]
[image]
name = "buttons/checkbox-pressed.png"
[/image]
{TEXT}
[/draw]
[/state_disabled_down]
[state_focussed_down]
full_redraw = "true"
[draw]
[image]
name = "buttons/checkbox-active-pressed.png"
[/image]
{TEXT}
[/draw]
[/state_focussed_down]
[/resolution]
[/toggle_button_definition]
#unddef TEXT

View file

@ -18,6 +18,7 @@ src/gui/widgets/settings.cpp
src/gui/widgets/spacer.cpp src/gui/widgets/spacer.cpp
src/gui/widgets/text_box.cpp src/gui/widgets/text_box.cpp
src/gui/widgets/text.cpp src/gui/widgets/text.cpp
src/gui/widgets/toggle_button.cpp
src/gui/widgets/tooltip.cpp src/gui/widgets/tooltip.cpp
src/gui/widgets/vertical_scrollbar.cpp src/gui/widgets/vertical_scrollbar.cpp
src/gui/widgets/widget.cpp src/gui/widgets/widget.cpp

View file

@ -155,6 +155,7 @@ SET(wesnoth-main_SRC
gui/widgets/scrollbar.cpp gui/widgets/scrollbar.cpp
gui/widgets/text.cpp gui/widgets/text.cpp
gui/widgets/text_box.cpp gui/widgets/text_box.cpp
gui/widgets/toggle_button.cpp
gui/widgets/tooltip.cpp gui/widgets/tooltip.cpp
gui/widgets/vertical_scrollbar.cpp gui/widgets/vertical_scrollbar.cpp
gui/widgets/widget.cpp gui/widgets/widget.cpp

View file

@ -81,6 +81,7 @@ wesnoth_source = \
gui/widgets/spacer.cpp \ gui/widgets/spacer.cpp \
gui/widgets/text.cpp \ gui/widgets/text.cpp \
gui/widgets/text_box.cpp \ gui/widgets/text_box.cpp \
gui/widgets/toggle_button.cpp \
gui/widgets/tooltip.cpp \ gui/widgets/tooltip.cpp \
gui/widgets/vertical_scrollbar.cpp \ gui/widgets/vertical_scrollbar.cpp \
gui/widgets/widget.cpp \ gui/widgets/widget.cpp \

View file

@ -95,6 +95,7 @@ libwesnoth_sources = Split("""
gui/widgets/spacer.cpp gui/widgets/spacer.cpp
gui/widgets/text.cpp gui/widgets/text.cpp
gui/widgets/text_box.cpp gui/widgets/text_box.cpp
gui/widgets/toggle_button.cpp
gui/widgets/tooltip.cpp gui/widgets/tooltip.cpp
gui/widgets/vertical_scrollbar.cpp gui/widgets/vertical_scrollbar.cpp
gui/widgets/widget.cpp gui/widgets/widget.cpp

View file

@ -172,6 +172,9 @@ const std::string& tgui_definition::read(const config& cfg)
* panel_definition A panel. * panel_definition A panel.
* spacer_definition A spacer. * spacer_definition A spacer.
* text_box_definition A single line text box. * text_box_definition A single line text box.
* toggle_button_definition A kind of button with two 'states' up and
* down. This is a more generic widget which
* is used for eg checkboxes and radioboxes.
* tooltip_definition A small tooltip with help. * tooltip_definition A small tooltip with help.
* vertical_scrollbar_definition A vertical scrollbar. * vertical_scrollbar_definition A vertical scrollbar.
* window_definition A window. * window_definition A window.
@ -198,6 +201,7 @@ const std::string& tgui_definition::read(const config& cfg)
load_definitions<tpanel_definition>("panel", cfg.get_children("panel_definition")); load_definitions<tpanel_definition>("panel", cfg.get_children("panel_definition"));
load_definitions<tspacer_definition>("spacer", cfg.get_children("spacer_definition")); load_definitions<tspacer_definition>("spacer", cfg.get_children("spacer_definition"));
load_definitions<ttext_box_definition>("text_box", cfg.get_children("text_box_definition")); load_definitions<ttext_box_definition>("text_box", cfg.get_children("text_box_definition"));
load_definitions<ttoggle_button_definition>("toggle_button", cfg.get_children("toggle_button_definition"));
load_definitions<ttooltip_definition>("tooltip", cfg.get_children("tooltip_definition")); load_definitions<ttooltip_definition>("tooltip", cfg.get_children("tooltip_definition"));
load_definitions<tvertical_scrollbar_definition> load_definitions<tvertical_scrollbar_definition>
("vertical_scrollbar", cfg.get_children("vertical_scrollbar_definition")); ("vertical_scrollbar", cfg.get_children("vertical_scrollbar_definition"));
@ -620,6 +624,45 @@ ttext_box_definition::tresolution::tresolution(const config& cfg) :
state.push_back(tstate_definition(cfg.child("state_focussed"))); state.push_back(tstate_definition(cfg.child("state_focussed")));
} }
ttoggle_button_definition::ttoggle_button_definition(const config& cfg) :
tcontrol_definition(cfg)
{
DBG_G_P << "Parsing toggle button " << id << '\n';
load_resolutions<tresolution>(cfg.get_children("resolution"));
}
ttoggle_button_definition::tresolution::tresolution(const config& cfg) :
tresolution_definition_(cfg)
{
/*WIKI
* @page = GUIToolkitWML
* @order = 1_widget_toggle_button
*
* == Toogle button ==
*
* The definition of a toggle button.
*
* The following states exist:
* * state_enabled_up, the button is enabled and in the upwards position.
* * state_disabled_up, the button is disabled and in the upwards position.
* * state_focussed_up, the mouse is over the button and the button is in the upwards position.
*
* The same three states exist with down for the downwardsposition.
*
*/
// Note the order should be the same as the enum tstate is toggle_button.hpp.
state.push_back(tstate_definition(cfg.child("state_enabled_up")));
state.push_back(tstate_definition(cfg.child("state_disabled_up")));
state.push_back(tstate_definition(cfg.child("state_focussed_up")));
state.push_back(tstate_definition(cfg.child("state_enabled_down")));
state.push_back(tstate_definition(cfg.child("state_disabled_down")));
state.push_back(tstate_definition(cfg.child("state_focussed_down")));
}
ttooltip_definition::ttooltip_definition(const config& cfg) : ttooltip_definition::ttooltip_definition(const config& cfg) :
tcontrol_definition(cfg) tcontrol_definition(cfg)
{ {

View file

@ -166,6 +166,16 @@ struct ttext_box_definition : public tcontrol_definition
}; };
struct ttoggle_button_definition : public tcontrol_definition
{
ttoggle_button_definition(const config& cfg);
struct tresolution : public tresolution_definition_
{
tresolution(const config& cfg);
};
};
struct ttooltip_definition : public tcontrol_definition struct ttooltip_definition : public tcontrol_definition
{ {
ttooltip_definition(const config& cfg); ttooltip_definition(const config& cfg);

View file

@ -0,0 +1,116 @@
/* $Id$ */
/*
copyright (C) 2008 by mark de wever <koraq@xs4all.nl>
part of the battle for wesnoth project http://www.wesnoth.org/
this program is free software; you can redistribute it and/or modify
it under the terms of the gnu general public license version 2
or at your option any later version.
this program is distributed in the hope that it will be useful,
but without any warranty.
see the copying file for more details.
*/
#include "gui/widgets/toggle_button.hpp"
#include "log.hpp"
#define DBG_G LOG_STREAM_INDENT(debug, gui)
#define LOG_G LOG_STREAM_INDENT(info, gui)
#define WRN_G LOG_STREAM_INDENT(warn, gui)
#define ERR_G LOG_STREAM_INDENT(err, gui)
#define DBG_G_D LOG_STREAM_INDENT(debug, gui_draw)
#define LOG_G_D LOG_STREAM_INDENT(info, gui_draw)
#define WRN_G_D LOG_STREAM_INDENT(warn, gui_draw)
#define ERR_G_D LOG_STREAM_INDENT(err, gui_draw)
#define DBG_G_E LOG_STREAM_INDENT(debug, gui_event)
#define LOG_G_E LOG_STREAM_INDENT(info, gui_event)
#define WRN_G_E LOG_STREAM_INDENT(warn, gui_event)
#define ERR_G_E LOG_STREAM_INDENT(err, gui_event)
#define DBG_G_P LOG_STREAM_INDENT(debug, gui_parse)
#define LOG_G_P LOG_STREAM_INDENT(info, gui_parse)
#define WRN_G_P LOG_STREAM_INDENT(warn, gui_parse)
#define ERR_G_P LOG_STREAM_INDENT(err, gui_parse)
namespace gui2 {
void ttoggle_button::mouse_enter(tevent_handler&)
{
DBG_G_E << "Toggle button: mouse enter.\n";
if(is_up()) {
set_state(FOCUSSED_UP);
} else {
set_state(FOCUSSED_DOWN);
}
}
void ttoggle_button::mouse_leave(tevent_handler&)
{
DBG_G_E << "Toggle button: mouse leave.\n";
if(is_up()) {
set_state(ENABLED_UP);
} else {
set_state(ENABLED_DOWN);
}
}
void ttoggle_button::mouse_left_button_click(tevent_handler&)
{
DBG_G_E << "Toggle button: left mouse button click.\n";
if(is_up()) {
set_state(ENABLED_DOWN);
} else {
set_state(ENABLED_UP);
}
// Do the custom handling (not implemented yet) FIXME
}
void ttoggle_button::set_active(const bool active)
{
if(active) {
if(is_up()) {
set_state(ENABLED_UP);
} else {
set_state(ENABLED_DOWN);
}
} else {
if(is_up()) {
set_state(DISABLED_UP);
} else {
set_state(DISABLED_DOWN);
}
}
}
void ttoggle_button::set_up(const bool up)
{
if(up == is_up()) {
return;
}
if(up) {
set_state(static_cast<tstate>(state_ - ENABLED_DOWN));
} else {
set_state(static_cast<tstate>(state_ + ENABLED_DOWN));
}
}
void ttoggle_button::set_state(tstate state)
{
if(state != state_) {
state_ = state;
set_dirty(true);
}
}
} // namespace gui2

View file

@ -0,0 +1,73 @@
/* $Id$ */
/*
copyright (C) 2008 by mark de wever <koraq@xs4all.nl>
part of the battle for wesnoth project http://www.wesnoth.org/
this program is free software; you can redistribute it and/or modify
it under the terms of the gnu general public license version 2
or at your option any later version.
this program is distributed in the hope that it will be useful,
but without any warranty.
see the copying file for more details.
*/
#ifndef __GUI_WIDGETS_TOGGLE_BUTTON_HPP_INCLUDED__
#define __GUI_WIDGETS_TOGGLE_BUTTON_HPP_INCLUDED__
#include "gui/widgets/control.hpp"
namespace gui2 {
// Class for a toggle button
class ttoggle_button : public tcontrol
{
public:
ttoggle_button() :
tcontrol(COUNT),
state_(ENABLED_UP)
{
load_config();
}
void mouse_enter(tevent_handler&);
void mouse_leave(tevent_handler&);
void mouse_left_button_click(tevent_handler&);
void set_active(const bool active);
bool get_active() const
{ return state_ != DISABLED_UP && state_ != DISABLED_DOWN; }
unsigned get_state() const { return state_; }
/** Is the widget in the up state. */
bool is_up() const { return state_ < ENABLED_DOWN; }
/** Set the button in the wanted state. */
void set_up(const bool up = true);
protected:
private:
//! Note the order of the states must be the same as defined in settings.hpp.
//! Also note the internals do assume the order for up and down to be the same
//! and also that 'up' is before 'down'.
enum tstate {
ENABLED_UP, DISABLED_UP, FOCUSSED_UP,
ENABLED_DOWN, DISABLED_DOWN, FOCUSSED_DOWN,
COUNT };
void set_state(tstate state);
tstate state_;
//! Inherited from tcontrol.
const std::string& get_control_type() const
{ static const std::string type = "toggle_button"; return type; }
};
} // namespace gui2
#endif

View file

@ -20,6 +20,7 @@
#include "gui/widgets/label.hpp" #include "gui/widgets/label.hpp"
#include "gui/widgets/spacer.hpp" #include "gui/widgets/spacer.hpp"
#include "gui/widgets/text_box.hpp" #include "gui/widgets/text_box.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/vertical_scrollbar.hpp" #include "gui/widgets/vertical_scrollbar.hpp"
#include "gui/widgets/widget.hpp" #include "gui/widgets/widget.hpp"
#include "gui/widgets/window.hpp" #include "gui/widgets/window.hpp"
@ -178,8 +179,30 @@ public:
{} {}
twidget* build () const; twidget* build () const;
} };
;
struct tbuilder_toggle_button : public tbuilder_control
{
private:
tbuilder_toggle_button();
public:
/*WIKI
* @page = GUIToolkitWML
* @order = 3_widget_toggle_button
*
* == Toggle button ==
*
* A toggle button has no special fields.
*
*/
tbuilder_toggle_button(const config& cfg) :
tbuilder_control(cfg)
{}
twidget* build () const;
};
struct tbuilder_vertical_scrollbar : public tbuilder_control struct tbuilder_vertical_scrollbar : public tbuilder_control
{ {
private: private:
@ -556,6 +579,8 @@ tbuilder_grid::tbuilder_grid(const config& cfg) :
widgets.push_back(new tbuilder_spacer(*((**col_itor).child("spacer")))); widgets.push_back(new tbuilder_spacer(*((**col_itor).child("spacer"))));
} else if((**col_itor).child("text_box")) { } else if((**col_itor).child("text_box")) {
widgets.push_back(new tbuilder_text_box(*((**col_itor).child("text_box")))); widgets.push_back(new tbuilder_text_box(*((**col_itor).child("text_box"))));
} else if((**col_itor).child("toggle_button")) {
widgets.push_back(new tbuilder_toggle_button(*((**col_itor).child("toggle_button"))));
} else if((**col_itor).child("vertical_scrollbar")) { } else if((**col_itor).child("vertical_scrollbar")) {
widgets.push_back( widgets.push_back(
new tbuilder_vertical_scrollbar(*((**col_itor).child("vertical_scrollbar")))); new tbuilder_vertical_scrollbar(*((**col_itor).child("vertical_scrollbar"))));
@ -681,7 +706,7 @@ tbuilder_button::tbuilder_button(const config& cfg) :
twidget* tbuilder_button::build() const twidget* tbuilder_button::build() const
{ {
tbutton *button = new tbutton(); tbutton* button = new tbutton();
init_control(button); init_control(button);
@ -699,7 +724,7 @@ twidget* tbuilder_button::build() const
twidget* tbuilder_label::build() const twidget* tbuilder_label::build() const
{ {
tlabel *tmp_label = new tlabel(); tlabel* tmp_label = new tlabel();
init_control(tmp_label); init_control(tmp_label);
@ -736,7 +761,7 @@ tbuilder_panel::tbuilder_panel(const config& cfg) :
twidget* tbuilder_panel::build() const twidget* tbuilder_panel::build() const
{ {
tpanel *panel = new tpanel(); tpanel* panel = new tpanel();
init_control(panel); init_control(panel);
@ -769,7 +794,7 @@ twidget* tbuilder_panel::build() const
twidget* tbuilder_spacer::build() const twidget* tbuilder_spacer::build() const
{ {
tspacer *spacer = new tspacer(); tspacer* spacer = new tspacer();
init_control(spacer); init_control(spacer);
@ -779,9 +804,21 @@ twidget* tbuilder_spacer::build() const
return spacer; return spacer;
} }
twidget* tbuilder_toggle_button::build() const
{
ttoggle_button *toggle_button = new ttoggle_button();
init_control(toggle_button);
DBG_G << "Window builder: placed toggle button '" << id << "' with defintion '"
<< definition << "'.\n";
return toggle_button;
}
twidget* tbuilder_text_box::build() const twidget* tbuilder_text_box::build() const
{ {
ttext_box *text_box = new ttext_box(); ttext_box* text_box = new ttext_box();
init_control(text_box); init_control(text_box);
@ -812,7 +849,7 @@ twidget* tbuilder_vertical_scrollbar::build() const
twidget* tbuilder_grid::build() const twidget* tbuilder_grid::build() const
{ {
tgrid *grid = new tgrid(); tgrid* grid = new tgrid();
grid->set_rows_cols(rows, cols); grid->set_rows_cols(rows, cols);