mirror of
https://github.com/clangen/musikcube.git
synced 2025-03-14 04:18:36 +00:00
Made Checkbox a subclass of TextLabel so we get styling (alignment,
weight, etc) for free.
This commit is contained in:
parent
7de549eb33
commit
c1dac7ef8d
@ -41,62 +41,51 @@
|
||||
|
||||
using namespace cursespp;
|
||||
|
||||
#define UNCHECKED std::string("[ ]")
|
||||
#define CHECKED std::string("[x]")
|
||||
static const std::string UNCHECKED = "[ ] ";
|
||||
static const std::string CHECKED = "[x] ";
|
||||
|
||||
static std::string decorate(const std::string& str, bool checked) {
|
||||
return (checked ? CHECKED : UNCHECKED) + str;
|
||||
}
|
||||
|
||||
Checkbox::Checkbox()
|
||||
: Window()
|
||||
: TextLabel()
|
||||
, checked(false) {
|
||||
this->SetFrameVisible(false);
|
||||
this->SetFocusedContentColor(Color::TextFocused);
|
||||
}
|
||||
|
||||
Checkbox::Checkbox(const std::string& value)
|
||||
: TextLabel(decorate(value, false))
|
||||
, originalText(value)
|
||||
, checked(checked) {
|
||||
}
|
||||
|
||||
Checkbox::Checkbox(const std::string& value, const text::TextAlign alignment)
|
||||
: TextLabel(decorate(value, false), alignment)
|
||||
, originalText(value) {
|
||||
}
|
||||
|
||||
Checkbox::~Checkbox() {
|
||||
}
|
||||
|
||||
void Checkbox::SetText(const std::string& value) {
|
||||
if (value != this->buffer) {
|
||||
this->buffer = value;
|
||||
this->Redraw();
|
||||
if (value != this->originalText) {
|
||||
this->originalText = value;
|
||||
TextLabel::SetText(decorate(value, this->checked));
|
||||
}
|
||||
}
|
||||
|
||||
std::string Checkbox::GetText() {
|
||||
return this->originalText;
|
||||
}
|
||||
|
||||
void Checkbox::SetChecked(bool checked) {
|
||||
if (checked != this->checked) {
|
||||
this->checked = checked;
|
||||
this->Redraw();
|
||||
TextLabel::SetText(decorate(this->originalText, checked));
|
||||
this->CheckChanged(this, checked);
|
||||
}
|
||||
}
|
||||
|
||||
void Checkbox::OnRedraw() {
|
||||
int cx = this->GetContentWidth();
|
||||
|
||||
if (cx > 0) {
|
||||
WINDOW* c = this->GetContent();
|
||||
werase(c);
|
||||
|
||||
int len = (int)u8cols(this->buffer);
|
||||
|
||||
std::string symbol = (this->checked ? CHECKED : UNCHECKED);
|
||||
std::string ellipsized = text::Ellipsize(symbol + " " + this->buffer, cx);
|
||||
|
||||
int64_t attrs = this->IsFocused()
|
||||
? this->GetFocusedContentColor()
|
||||
: this->GetContentColor();
|
||||
|
||||
if (attrs != -1) {
|
||||
wattron(c, attrs);
|
||||
}
|
||||
|
||||
checked_wprintw(c, ellipsized.c_str());
|
||||
|
||||
if (attrs != -1) {
|
||||
wattroff(c, attrs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Checkbox::KeyPress(const std::string& key) {
|
||||
if (key == " " || key == "KEY_ENTER") {
|
||||
this->SetChecked(!this->checked);
|
||||
|
@ -79,10 +79,16 @@ void TextLabel::OnRedraw() {
|
||||
|
||||
WINDOW* c = this->GetContent();
|
||||
|
||||
Color color = this->IsFocused()
|
||||
? this->GetFocusedContentColor()
|
||||
: this->GetContentColor();
|
||||
|
||||
wattron(c, color);
|
||||
if (this->bold) { wattron(c, A_BOLD); }
|
||||
wmove(c, 0, 0);
|
||||
checked_waddstr(c, aligned.c_str());
|
||||
if (this->bold) { wattroff(c, A_BOLD); }
|
||||
wattroff(c, color);
|
||||
}
|
||||
|
||||
void TextLabel::SetText(const std::string& value, const text::TextAlign alignment) {
|
||||
@ -93,6 +99,13 @@ void TextLabel::SetText(const std::string& value, const text::TextAlign alignmen
|
||||
}
|
||||
}
|
||||
|
||||
void TextLabel::SetAlignment(const text::TextAlign alignment) {
|
||||
if (this->alignment != alignment) {
|
||||
this->alignment = alignment;
|
||||
this->Redraw();
|
||||
}
|
||||
}
|
||||
|
||||
void TextLabel::SetText(const std::string& value) {
|
||||
this->SetText(value, this->alignment);
|
||||
}
|
||||
|
@ -35,32 +35,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <cursespp/curses_config.h>
|
||||
#include <cursespp/Window.h>
|
||||
#include <cursespp/TextLabel.h>
|
||||
#include <cursespp/IKeyHandler.h>
|
||||
#include <sigslot/sigslot.h>
|
||||
|
||||
namespace cursespp {
|
||||
class Checkbox :
|
||||
public cursespp::IKeyHandler,
|
||||
public cursespp::Window
|
||||
{
|
||||
class Checkbox: public cursespp::TextLabel {
|
||||
public:
|
||||
sigslot::signal2<Checkbox*, bool> CheckChanged;
|
||||
|
||||
Checkbox();
|
||||
Checkbox(const std::string& value);
|
||||
Checkbox(const std::string& value, const text::TextAlign alignment);
|
||||
|
||||
virtual ~Checkbox();
|
||||
|
||||
virtual void SetText(const std::string& value);
|
||||
virtual std::string GetText();
|
||||
virtual void SetChecked(bool checked);
|
||||
virtual std::string GetText() { return this->buffer; }
|
||||
virtual bool IsChecked() { return this->checked; }
|
||||
virtual bool KeyPress(const std::string& key);
|
||||
virtual bool MouseEvent(const IMouseHandler::Event& event);
|
||||
virtual void OnRedraw();
|
||||
|
||||
private:
|
||||
|
||||
std::string buffer;
|
||||
bool checked;
|
||||
std::string originalText;
|
||||
};
|
||||
}
|
||||
|
@ -63,6 +63,7 @@ namespace cursespp {
|
||||
|
||||
virtual std::string GetText() { return this->buffer; }
|
||||
virtual size_t Length() { return u8cols(this->buffer); }
|
||||
virtual void SetAlignment(const text::TextAlign alignment);
|
||||
virtual void SetBold(bool bold);
|
||||
virtual bool IsBold() { return this->bold; }
|
||||
virtual void OnRedraw();
|
||||
|
Loading…
x
Reference in New Issue
Block a user