File: checkbox.h

package info (click to toggle)
widelands 1%3A19%2Brepack-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 371,172 kB
  • sloc: cpp: 108,458; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 229
file content (138 lines) | stat: -rw-r--r-- 3,841 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2004-2016 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef WL_UI_BASIC_CHECKBOX_H
#define WL_UI_BASIC_CHECKBOX_H

#include <boost/signals2.hpp>

#include "graphic/color.h"
#include "ui_basic/panel.h"

constexpr int kStateboxSize = 20;

namespace UI {

/**
 * Virtual base class providing a box that can be checked or unchecked.
 * Serves as base for Checkbox and Radiobutton.
 */
struct Statebox : public Panel {

	/**
	 * Pictorial Statebox
	 */
	Statebox(Panel* parent,
	         Point,
	         const Image* pic,
	         const std::string& tooltip_text = std::string());

	/**
	 * Textual Statebox
	 * If width is set to 0, the checkbox will set its width automatically.
	 * Otherwise, it will take up multiple lines if necessary (automatic height).
	 */
	Statebox(Panel* parent,
	         Point,
	         const std::string& label_text,
	         const std::string& tooltip_text = std::string(),
	         uint32_t width = 0);
	~Statebox();

	boost::signals2::signal<void()> changed;
	boost::signals2::signal<void(bool)> changedto;
	boost::signals2::signal<void(bool)> clickedto;  // same as changedto but only called when clicked

	void set_enabled(bool enabled);

	bool get_state() const {
		return flags_ & Is_Checked;
	}
	void set_state(bool on);

	void set_owns_custopicture_() {
		assert(flags_ & Has_Custom_Picture);
		set_flags(Owns_Custom_Picture, true);
	}

	// Drawing and event handlers
	void draw(RenderTarget&) override;

	void handle_mousein(bool inside) override;
	bool handle_mousepress(uint8_t btn, int32_t x, int32_t y) override;
	bool handle_mouserelease(uint8_t btn, int32_t x, int32_t y) override;
	bool handle_mousemove(uint8_t, int32_t, int32_t, int32_t, int32_t) override;

private:
	virtual void clicked() = 0;

	enum Flags {
		Is_Highlighted = 0x01,
		Is_Enabled = 0x02,
		Is_Checked = 0x04,
		Has_Custom_Picture = 0x08,
		Owns_Custom_Picture = 0x10
	};
	uint8_t flags_;
	void set_flags(uint8_t const flags, bool const enable) {
		flags_ &= ~flags;
		if (enable)
			flags_ |= flags;
	}
	const Image* pic_graphics_;
	const Image* rendered_text_;
};

/**
 * A checkbox is a simplistic panel which consists of just a small box which
 * can be either checked (on) or unchecked (off)
 * A checkbox only differs from a Statebox in that clicking on it toggles the
 * state
*/
struct Checkbox : public Statebox {

	/**
	 * Pictorial Checkbox
	 */
	Checkbox(Panel* const parent,
	         Point const p,
	         const Image* pic,
	         const std::string& tooltip_text = std::string())
	   : Statebox(parent, p, pic, tooltip_text) {
	}

	/**
	 * Textual Checkbox
	 * If width is set to 0, the checkbox will set its width automatically.
	 * Otherwise, it will take up multiple lines if necessary (automatic height).
	 */
	Checkbox(Panel* const parent,
	         Point const p,
	         const std::string& label_text,
	         const std::string& tooltip_text = std::string(),
	         uint32_t width = 0)
	   : Statebox(parent, p, label_text, tooltip_text, width) {
	}

private:
	void clicked() override;
};
}

#endif  // end of include guard: WL_UI_BASIC_CHECKBOX_H