2017-07-30 04:39:01 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2017 Furrtek
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* 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, 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; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_tabview.hpp"
|
2023-06-07 11:33:32 -04:00
|
|
|
#include "ui_font_fixed_8x16.hpp"
|
2017-07-30 04:39:01 -04:00
|
|
|
|
|
|
|
#include "portapack.hpp"
|
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
Tab::Tab() {
|
2023-05-18 16:16:05 -04:00
|
|
|
set_focusable(true);
|
2017-07-30 04:39:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
void Tab::set(
|
2023-05-18 16:16:05 -04:00
|
|
|
uint32_t index,
|
|
|
|
Dim width,
|
|
|
|
std::string text,
|
|
|
|
Color text_color) {
|
|
|
|
set_parent_rect({(Coord)(index * width), 0, width, 24});
|
|
|
|
|
|
|
|
text_ = text.substr(0, (width - 8) / 8);
|
|
|
|
text_color_ = text_color;
|
|
|
|
|
|
|
|
index_ = index;
|
2017-07-30 04:39:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tab::paint(Painter& painter) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto rect = screen_rect();
|
2024-05-27 15:02:52 -04:00
|
|
|
const Color color = highlighted() ? Theme::getInstance()->bg_darkest->background : Theme::getInstance()->bg_medium->background;
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
painter.fill_rectangle({rect.left(), rect.top(), rect.width() - 8, rect.height()}, color);
|
|
|
|
|
|
|
|
if (!highlighted())
|
2024-05-27 15:02:52 -04:00
|
|
|
painter.draw_hline({rect.left(), rect.top()}, rect.width() - 9, Theme::getInstance()->fg_light->foreground);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
painter.draw_bitmap(
|
|
|
|
{rect.right() - 8, rect.top()},
|
|
|
|
bitmap_tab_edge,
|
|
|
|
color,
|
2024-05-27 15:02:52 -04:00
|
|
|
Theme::getInstance()->bg_dark->background);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
auto text_point = rect.center() - Point(4, 0) - Point(text_.size() * 8 / 2, 16 / 2);
|
|
|
|
|
|
|
|
painter.draw_string(
|
|
|
|
text_point,
|
|
|
|
{ui::font::fixed_8x16, color, text_color_},
|
|
|
|
text_);
|
|
|
|
|
|
|
|
if (has_focus())
|
2024-05-27 15:02:52 -04:00
|
|
|
painter.draw_hline(text_point + Point(0, 16), text_.size() * 8, Theme::getInstance()->bg_darkest->foreground);
|
2017-07-30 04:39:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Tab::on_key(const KeyEvent key) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (key == KeyEvent::Select) {
|
|
|
|
static_cast<TabView*>(parent())->set_selected(index_);
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-30 04:39:01 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
return false;
|
2017-07-30 04:39:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Tab::on_touch(const TouchEvent event) {
|
2023-05-18 16:16:05 -04:00
|
|
|
switch (event.type) {
|
|
|
|
case TouchEvent::Type::Start:
|
|
|
|
focus();
|
|
|
|
set_dirty();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case TouchEvent::Type::End:
|
|
|
|
static_cast<TabView*>(parent())->set_selected(index_);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-30 04:39:01 -04:00
|
|
|
}
|
|
|
|
|
2017-09-21 04:18:17 -04:00
|
|
|
void TabView::set_selected(uint32_t index) {
|
2023-05-18 16:16:05 -04:00
|
|
|
Tab* tab;
|
|
|
|
|
|
|
|
if (index >= n_tabs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Hide previous view
|
|
|
|
views[current_tab]->hidden(true);
|
|
|
|
|
|
|
|
tab = &tabs[current_tab];
|
|
|
|
tab->set_highlighted(false);
|
|
|
|
tab->set_focusable(true);
|
|
|
|
tab->set_dirty();
|
|
|
|
|
|
|
|
// Show new view
|
|
|
|
views[index]->hidden(false);
|
|
|
|
|
|
|
|
tab = &tabs[index];
|
|
|
|
current_tab = index;
|
|
|
|
tab->set_highlighted(true);
|
|
|
|
tab->set_focusable(false);
|
|
|
|
tab->set_dirty();
|
2017-07-30 04:39:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabView::on_show() {
|
2023-05-18 16:16:05 -04:00
|
|
|
set_selected(current_tab);
|
2017-07-30 04:39:01 -04:00
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2017-07-30 09:46:42 -04:00
|
|
|
void TabView::focus() {
|
2023-05-18 16:16:05 -04:00
|
|
|
views[current_tab]->focus();
|
2017-07-30 09:46:42 -04:00
|
|
|
}
|
2017-07-30 04:39:01 -04:00
|
|
|
|
|
|
|
TabView::TabView(std::initializer_list<TabDef> tab_definitions) {
|
2023-05-18 16:16:05 -04:00
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
n_tabs = tab_definitions.size();
|
|
|
|
if (n_tabs > MAX_TABS)
|
|
|
|
n_tabs = MAX_TABS;
|
|
|
|
|
|
|
|
size_t tab_width = 240 / n_tabs;
|
|
|
|
|
|
|
|
set_parent_rect({0, 0, 30 * 8, 3 * 8});
|
|
|
|
|
|
|
|
for (auto& tab_definition : tab_definitions) {
|
|
|
|
tabs[i].set(i, tab_width, tab_definition.text, tab_definition.color);
|
|
|
|
views[i] = tab_definition.view;
|
|
|
|
add_child(&tabs[i]);
|
|
|
|
i++;
|
|
|
|
if (i == MAX_TABS) break;
|
|
|
|
}
|
2017-07-30 04:39:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TabView::~TabView() {
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|