2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-09-13 23:48:22 +03:00
|
|
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-04-03 16:23:13 +03:00
|
|
|
#include "GlyphMapWidget.h"
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Painter.h>
|
2020-12-31 16:01:59 +03:00
|
|
|
#include <LibGfx/BitmapFont.h>
|
2020-02-15 01:53:11 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-04-03 16:23:13 +03:00
|
|
|
|
2021-04-06 18:58:08 +03:00
|
|
|
GlyphMapWidget::GlyphMapWidget()
|
2019-04-03 16:23:13 +03:00
|
|
|
{
|
2020-10-30 12:58:27 +03:00
|
|
|
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
2021-04-06 18:58:08 +03:00
|
|
|
horizontal_scrollbar().set_visible(false);
|
2019-04-03 16:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GlyphMapWidget::~GlyphMapWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-06 18:58:08 +03:00
|
|
|
void GlyphMapWidget::initialize(Gfx::BitmapFont& mutable_font)
|
2019-04-03 16:23:13 +03:00
|
|
|
{
|
2021-04-06 18:58:08 +03:00
|
|
|
if (m_font == mutable_font)
|
|
|
|
return;
|
|
|
|
m_font = mutable_font;
|
|
|
|
vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
|
2021-09-22 00:24:09 +03:00
|
|
|
set_selected_glyph('A');
|
2019-04-03 16:23:13 +03:00
|
|
|
}
|
|
|
|
|
2021-04-06 18:58:08 +03:00
|
|
|
void GlyphMapWidget::resize_event(GUI::ResizeEvent& event)
|
2019-04-03 16:23:13 +03:00
|
|
|
{
|
2021-11-29 17:07:47 +03:00
|
|
|
if (!m_font)
|
|
|
|
return;
|
|
|
|
|
2021-11-29 18:45:55 +03:00
|
|
|
int event_width = event.size().width() - vertical_scrollbar().width() - (frame_thickness() * 2) - m_horizontal_spacing;
|
2021-09-09 14:41:08 +03:00
|
|
|
int event_height = event.size().height() - (frame_thickness() * 2);
|
|
|
|
m_visible_glyphs = (event_width * event_height) / (font().max_glyph_width() * font().glyph_height());
|
2021-04-06 18:58:08 +03:00
|
|
|
m_columns = max(event_width / (font().max_glyph_width() + m_horizontal_spacing), 1);
|
|
|
|
m_rows = ceil_div(m_glyph_count, m_columns);
|
|
|
|
|
|
|
|
int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
|
|
|
|
int content_height = rows() * (font().glyph_height() + m_vertical_spacing) + frame_thickness();
|
|
|
|
set_content_size({ content_width, content_height });
|
|
|
|
|
2021-09-22 16:59:48 +03:00
|
|
|
scroll_to_glyph(m_selected_glyph);
|
|
|
|
|
2021-05-03 21:31:58 +03:00
|
|
|
AbstractScrollableWidget::resize_event(event);
|
2019-04-03 16:23:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-09 01:06:56 +03:00
|
|
|
void GlyphMapWidget::set_selected_glyph(int glyph)
|
2019-04-03 16:23:13 +03:00
|
|
|
{
|
|
|
|
if (m_selected_glyph == glyph)
|
|
|
|
return;
|
|
|
|
m_selected_glyph = glyph;
|
|
|
|
if (on_glyph_selected)
|
|
|
|
on_glyph_selected(glyph);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect GlyphMapWidget::get_outer_rect(int glyph) const
|
2019-04-03 16:23:13 +03:00
|
|
|
{
|
|
|
|
int row = glyph / columns();
|
|
|
|
int column = glyph % columns();
|
2020-06-10 11:57:59 +03:00
|
|
|
return Gfx::IntRect {
|
2019-04-03 16:23:13 +03:00
|
|
|
column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
|
|
|
|
row * (font().glyph_height() + m_vertical_spacing) + 1,
|
|
|
|
font().max_glyph_width() + m_horizontal_spacing,
|
|
|
|
font().glyph_height() + m_horizontal_spacing
|
2019-06-07 12:48:03 +03:00
|
|
|
}
|
2021-04-06 18:58:08 +03:00
|
|
|
.translated(frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value());
|
2019-04-03 16:23:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-09 01:06:56 +03:00
|
|
|
void GlyphMapWidget::update_glyph(int glyph)
|
2019-04-06 16:28:06 +03:00
|
|
|
{
|
|
|
|
update(get_outer_rect(glyph));
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void GlyphMapWidget::paint_event(GUI::PaintEvent& event)
|
2019-04-03 16:23:13 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::Frame::paint_event(event);
|
2019-04-10 03:43:57 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::Painter painter(*this);
|
2021-04-06 18:58:08 +03:00
|
|
|
painter.add_clip_rect(widget_inner_rect());
|
2019-04-06 16:28:06 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
2019-04-03 16:23:13 +03:00
|
|
|
painter.set_font(font());
|
2021-04-18 21:12:52 +03:00
|
|
|
painter.fill_rect(widget_inner_rect(), palette().inactive_window_title());
|
2019-04-10 03:43:57 +03:00
|
|
|
|
2021-11-29 18:45:55 +03:00
|
|
|
int scroll_steps = vertical_scrollbar().value() / vertical_scrollbar().step();
|
2021-09-09 14:41:08 +03:00
|
|
|
int first_visible_glyph = scroll_steps * columns();
|
|
|
|
|
|
|
|
for (int glyph = first_visible_glyph; glyph <= first_visible_glyph + m_visible_glyphs && glyph < m_glyph_count; ++glyph) {
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect outer_rect = get_outer_rect(glyph);
|
|
|
|
Gfx::IntRect inner_rect(
|
2020-02-23 14:14:02 +03:00
|
|
|
outer_rect.x() + m_horizontal_spacing / 2,
|
|
|
|
outer_rect.y() + m_vertical_spacing / 2,
|
|
|
|
font().max_glyph_width(),
|
|
|
|
font().glyph_height());
|
|
|
|
if (glyph == m_selected_glyph) {
|
|
|
|
painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
|
2021-11-29 16:43:32 +03:00
|
|
|
if (m_font->contains_raw_glyph(glyph))
|
2021-04-22 21:25:28 +03:00
|
|
|
painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
|
2021-11-29 16:43:32 +03:00
|
|
|
} else if (m_font->contains_raw_glyph(glyph)) {
|
2021-04-18 21:12:52 +03:00
|
|
|
painter.fill_rect(outer_rect, palette().base());
|
2020-02-23 14:14:02 +03:00
|
|
|
painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
|
2019-04-03 16:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void GlyphMapWidget::mousedown_event(GUI::MouseEvent& event)
|
2019-04-03 16:23:13 +03:00
|
|
|
{
|
2020-02-23 14:14:02 +03:00
|
|
|
GUI::Frame::mousedown_event(event);
|
|
|
|
|
2021-09-06 03:36:10 +03:00
|
|
|
Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
|
|
|
|
auto map_position = event.position() - map_offset;
|
|
|
|
auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing));
|
|
|
|
auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing));
|
|
|
|
auto glyph = row * columns() + col;
|
|
|
|
if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count) {
|
|
|
|
set_selected_glyph(glyph);
|
2019-04-03 16:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-23 14:14:02 +03:00
|
|
|
|
|
|
|
void GlyphMapWidget::keydown_event(GUI::KeyEvent& event)
|
|
|
|
{
|
|
|
|
GUI::Frame::keydown_event(event);
|
|
|
|
|
|
|
|
if (event.key() == KeyCode::Key_Up) {
|
|
|
|
if (selected_glyph() >= m_columns) {
|
|
|
|
set_selected_glyph(selected_glyph() - m_columns);
|
2021-04-06 18:58:08 +03:00
|
|
|
scroll_to_glyph(selected_glyph());
|
2020-02-23 14:14:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (event.key() == KeyCode::Key_Down) {
|
|
|
|
if (selected_glyph() < m_glyph_count - m_columns) {
|
|
|
|
set_selected_glyph(selected_glyph() + m_columns);
|
2021-04-06 18:58:08 +03:00
|
|
|
scroll_to_glyph(selected_glyph());
|
2020-02-23 14:14:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (event.key() == KeyCode::Key_Left) {
|
|
|
|
if (selected_glyph() > 0) {
|
|
|
|
set_selected_glyph(selected_glyph() - 1);
|
2021-04-06 18:58:08 +03:00
|
|
|
scroll_to_glyph(selected_glyph());
|
2020-02-23 14:14:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (event.key() == KeyCode::Key_Right) {
|
|
|
|
if (selected_glyph() < m_glyph_count - 1) {
|
|
|
|
set_selected_glyph(selected_glyph() + 1);
|
2021-04-06 18:58:08 +03:00
|
|
|
scroll_to_glyph(selected_glyph());
|
2020-02-23 14:14:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (event.ctrl() && event.key() == KeyCode::Key_Home) {
|
|
|
|
set_selected_glyph(0);
|
2021-04-06 18:58:08 +03:00
|
|
|
scroll_to_glyph(selected_glyph());
|
2020-02-23 14:14:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.ctrl() && event.key() == KeyCode::Key_End) {
|
|
|
|
set_selected_glyph(m_glyph_count - 1);
|
2021-04-06 18:58:08 +03:00
|
|
|
scroll_to_glyph(selected_glyph());
|
2020-02-23 14:14:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
|
|
|
|
set_selected_glyph(selected_glyph() / m_columns * m_columns);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!event.ctrl() && event.key() == KeyCode::Key_End) {
|
|
|
|
int new_selection = selected_glyph() / m_columns * m_columns + (m_columns - 1);
|
2020-05-09 01:06:56 +03:00
|
|
|
int max = m_glyph_count - 1;
|
|
|
|
new_selection = clamp(new_selection, 0, max);
|
2020-02-23 14:14:02 +03:00
|
|
|
set_selected_glyph(new_selection);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 18:58:08 +03:00
|
|
|
|
|
|
|
void GlyphMapWidget::scroll_to_glyph(int glyph)
|
|
|
|
{
|
|
|
|
int row = glyph / columns();
|
|
|
|
int column = glyph % columns();
|
|
|
|
auto scroll_rect = Gfx::IntRect {
|
|
|
|
column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
|
|
|
|
row * (font().glyph_height() + m_vertical_spacing) + 1,
|
|
|
|
font().max_glyph_width() + m_horizontal_spacing,
|
|
|
|
font().glyph_height() + m_horizontal_spacing
|
|
|
|
};
|
|
|
|
scroll_into_view(scroll_rect, true, true);
|
|
|
|
}
|