mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +00:00
LibGUI: Add on_glyph_double_clicked() callback to GlyphMapWidget
This will be used by CharacterMap. In implementing this, extracted the logic for finding which glyph is at a given position within the widget.
This commit is contained in:
parent
2327ea8970
commit
ff500ffcc4
2 changed files with 27 additions and 6 deletions
|
@ -135,16 +135,25 @@ void GlyphMapWidget::paint_event(PaintEvent& event)
|
||||||
painter.draw_focus_rect(get_outer_rect(m_active_glyph), Gfx::Color::Black);
|
painter.draw_focus_rect(get_outer_rect(m_active_glyph), Gfx::Color::Black);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<int> GlyphMapWidget::glyph_at_position(Gfx::IntPoint position) const
|
||||||
|
{
|
||||||
|
Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
|
||||||
|
auto map_position = 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)
|
||||||
|
return glyph;
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
void GlyphMapWidget::mousedown_event(MouseEvent& event)
|
void GlyphMapWidget::mousedown_event(MouseEvent& event)
|
||||||
{
|
{
|
||||||
Frame::mousedown_event(event);
|
Frame::mousedown_event(event);
|
||||||
|
|
||||||
Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
|
if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) {
|
||||||
auto map_position = event.position() - map_offset;
|
auto glyph = maybe_glyph.value();
|
||||||
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) {
|
|
||||||
if (event.shift())
|
if (event.shift())
|
||||||
m_selection.extend_to(glyph);
|
m_selection.extend_to(glyph);
|
||||||
else {
|
else {
|
||||||
|
@ -155,6 +164,15 @@ void GlyphMapWidget::mousedown_event(MouseEvent& event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GlyphMapWidget::doubleclick_event(MouseEvent& event)
|
||||||
|
{
|
||||||
|
Widget::doubleclick_event(event);
|
||||||
|
if (on_glyph_double_clicked) {
|
||||||
|
if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value())
|
||||||
|
on_glyph_double_clicked(maybe_glyph.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void GlyphMapWidget::keydown_event(KeyEvent& event)
|
void GlyphMapWidget::keydown_event(KeyEvent& event)
|
||||||
{
|
{
|
||||||
Frame::keydown_event(event);
|
Frame::keydown_event(event);
|
||||||
|
|
|
@ -60,16 +60,19 @@ public:
|
||||||
int columns() const { return m_columns; }
|
int columns() const { return m_columns; }
|
||||||
|
|
||||||
Function<void(int)> on_active_glyph_changed;
|
Function<void(int)> on_active_glyph_changed;
|
||||||
|
Function<void(int)> on_glyph_double_clicked;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GlyphMapWidget();
|
GlyphMapWidget();
|
||||||
virtual void paint_event(PaintEvent&) override;
|
virtual void paint_event(PaintEvent&) override;
|
||||||
virtual void mousedown_event(MouseEvent&) override;
|
virtual void mousedown_event(MouseEvent&) override;
|
||||||
|
virtual void doubleclick_event(MouseEvent&) override;
|
||||||
virtual void keydown_event(KeyEvent&) override;
|
virtual void keydown_event(KeyEvent&) override;
|
||||||
virtual void resize_event(ResizeEvent&) override;
|
virtual void resize_event(ResizeEvent&) override;
|
||||||
virtual void did_change_font() override;
|
virtual void did_change_font() override;
|
||||||
|
|
||||||
Gfx::IntRect get_outer_rect(int glyph) const;
|
Gfx::IntRect get_outer_rect(int glyph) const;
|
||||||
|
Optional<int> glyph_at_position(Gfx::IntPoint) const;
|
||||||
|
|
||||||
void recalculate_content_size();
|
void recalculate_content_size();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue