FTXUI/include/ftxui/component/checkbox.hpp

61 lines
1.9 KiB
C++
Raw Normal View History

2019-01-13 05:25:49 +08:00
#ifndef FTXUI_COMPONENT_CHECKBOX_HPP
#define FTXUI_COMPONENT_CHECKBOX_HPP
2021-05-10 02:32:27 +08:00
#include <functional> // for function
#include <string> // for wstring, allocator
2019-01-13 05:25:49 +08:00
2021-05-10 02:32:27 +08:00
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing
#include "ftxui/screen/box.hpp" // for Box
2019-01-13 05:25:49 +08:00
namespace ftxui {
2021-05-02 02:40:35 +08:00
struct Event;
2019-01-13 05:25:49 +08:00
2020-09-06 19:46:56 +08:00
/// @brief A Checkbox. It can be checked or unchecked.Display an element on a
/// ftxui::Screen.
2020-08-16 08:24:50 +08:00
/// @ingroup dom
2021-05-10 02:32:27 +08:00
class CheckboxBase : public ComponentBase {
2019-01-13 05:25:49 +08:00
public:
2021-05-10 02:32:27 +08:00
// Access this interface from a Component
static CheckboxBase* From(Component component);
2019-01-13 05:25:49 +08:00
2021-05-10 02:32:27 +08:00
// Constructor.
CheckboxBase(const std::wstring* label, bool* state);
~CheckboxBase() override = default;
2019-01-13 05:25:49 +08:00
#if defined(_WIN32)
2020-09-06 19:46:56 +08:00
std::wstring checked = L"[X] "; /// Prefix for a "checked" state.
std::wstring unchecked = L"[ ] "; /// Prefix for an "unchecked" state.
#else
2020-09-06 19:46:56 +08:00
std::wstring checked = L""; /// Prefix for a "checked" state.
std::wstring unchecked = L""; /// Prefix for a "unchecked" state.
#endif
2019-01-13 05:25:49 +08:00
2020-09-06 19:46:56 +08:00
Decorator focused_style = inverted; /// Decorator used when focused.
Decorator unfocused_style = nothing; /// Decorator used when unfocused.
2019-01-19 07:20:29 +08:00
2021-05-10 02:32:27 +08:00
/// Called when the user change the state of the CheckboxBase.
std::function<void()> on_change = []() {};
2019-01-13 05:25:49 +08:00
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
private:
bool OnMouseEvent(Event event);
2021-05-10 02:32:27 +08:00
const std::wstring* const label_;
bool* const state_;
2019-01-13 05:25:49 +08:00
int cursor_position = 0;
Box box_;
2019-01-13 05:25:49 +08:00
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_CHECKBOX_HPP */
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.