Added text alignment options to ShortcutsWindow.

This commit is contained in:
casey 2016-09-02 11:59:05 -07:00
parent 7e60827aab
commit 38912b9e63
2 changed files with 39 additions and 2 deletions

View File

@ -43,7 +43,8 @@
using namespace cursespp;
ShortcutsWindow::ShortcutsWindow()
: Window(nullptr) {
: Window(nullptr)
, alignment(text::AlignCenter) {
this->SetFrameVisible(false);
this->UpdateContentColor();
}
@ -51,6 +52,11 @@ ShortcutsWindow::ShortcutsWindow()
ShortcutsWindow::~ShortcutsWindow() {
}
void ShortcutsWindow::SetAlignment(text::TextAlign align) {
this->alignment = align;
this->Repaint();
}
void ShortcutsWindow::AddShortcut(
const std::string& key,
const std::string& description,
@ -83,6 +89,29 @@ void ShortcutsWindow::UpdateContentColor() {
: CURSESPP_SHORTCUT_ROW_NORMAL);
}
size_t ShortcutsWindow::CalculateLeftPadding() {
if (this->alignment = text::AlignLeft) {
return 0;
}
int padding = this->GetContentWidth();
for (size_t i = 0; i < this->entries.size(); i++) {
auto e = this->entries[i];
padding -= u8cols(e->key) + u8cols(e->description) + 5;
}
if (padding < 0) {
return 0;
}
if (this->alignment == text::AlignRight) {
return padding;
}
return (padding / 2); /* text::AlignCenter */
}
void ShortcutsWindow::Repaint() {
Window::Repaint();
@ -93,6 +122,9 @@ void ShortcutsWindow::Repaint() {
WINDOW* c = this->GetContent();
size_t leftPadding = this->CalculateLeftPadding();
wmove(c, 0, leftPadding);
size_t remaining = this->GetContentWidth();
for (size_t i = 0; i < this->entries.size() && remaining > 0; i++) {
auto e = this->entries[i];

View File

@ -34,7 +34,8 @@
#pragma once
#include <cursespp/Window.h>
#include "Window.h"
#include "Text.h"
namespace cursespp {
class ShortcutsWindow :
@ -47,6 +48,8 @@ namespace cursespp {
ShortcutsWindow();
virtual ~ShortcutsWindow();
void SetAlignment(text::TextAlign alignment);
void AddShortcut(
const std::string& key,
const std::string& description,
@ -63,6 +66,7 @@ namespace cursespp {
private:
void UpdateContentColor();
size_t CalculateLeftPadding();
struct Entry {
Entry(const std::string& key, const std::string& desc, int64 attrs = -1) {
@ -80,5 +84,6 @@ namespace cursespp {
EntryList entries;
std::string activeKey;
text::TextAlign alignment;
};
}