Serenity Operating System
at hosted 165 lines 5.6 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "GlyphMapWidget.h" 28#include <LibGUI/Painter.h> 29#include <LibGfx/Font.h> 30#include <LibGfx/Palette.h> 31 32GlyphMapWidget::GlyphMapWidget(Gfx::Font& mutable_font) 33 : m_font(mutable_font) 34{ 35 set_relative_rect({ 0, 0, preferred_width(), preferred_height() }); 36} 37 38GlyphMapWidget::~GlyphMapWidget() 39{ 40} 41 42int GlyphMapWidget::preferred_width() const 43{ 44 return columns() * (font().max_glyph_width() + m_horizontal_spacing) + 2 + frame_thickness() * 2; 45} 46 47int GlyphMapWidget::preferred_height() const 48{ 49 return rows() * (font().glyph_height() + m_vertical_spacing) + 2 + frame_thickness() * 2; 50} 51 52void GlyphMapWidget::set_selected_glyph(u8 glyph) 53{ 54 if (m_selected_glyph == glyph) 55 return; 56 m_selected_glyph = glyph; 57 if (on_glyph_selected) 58 on_glyph_selected(glyph); 59 update(); 60} 61 62Gfx::Rect GlyphMapWidget::get_outer_rect(u8 glyph) const 63{ 64 int row = glyph / columns(); 65 int column = glyph % columns(); 66 return Gfx::Rect { 67 column * (font().max_glyph_width() + m_horizontal_spacing) + 1, 68 row * (font().glyph_height() + m_vertical_spacing) + 1, 69 font().max_glyph_width() + m_horizontal_spacing, 70 font().glyph_height() + m_horizontal_spacing 71 } 72 .translated(frame_thickness(), frame_thickness()); 73} 74 75void GlyphMapWidget::update_glyph(u8 glyph) 76{ 77 update(get_outer_rect(glyph)); 78} 79 80void GlyphMapWidget::paint_event(GUI::PaintEvent& event) 81{ 82 GUI::Frame::paint_event(event); 83 84 GUI::Painter painter(*this); 85 painter.add_clip_rect(event.rect()); 86 87 painter.set_font(font()); 88 painter.fill_rect(frame_inner_rect(), palette().base()); 89 90 for (int glyph = 0; glyph < m_glyph_count; ++glyph) { 91 Gfx::Rect outer_rect = get_outer_rect(glyph); 92 Gfx::Rect inner_rect( 93 outer_rect.x() + m_horizontal_spacing / 2, 94 outer_rect.y() + m_vertical_spacing / 2, 95 font().max_glyph_width(), 96 font().glyph_height()); 97 if (glyph == m_selected_glyph) { 98 painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection()); 99 painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text()); 100 } else { 101 painter.draw_glyph(inner_rect.location(), glyph, palette().base_text()); 102 } 103 } 104} 105 106void GlyphMapWidget::mousedown_event(GUI::MouseEvent& event) 107{ 108 GUI::Frame::mousedown_event(event); 109 110 // FIXME: This is a silly loop. 111 for (int glyph = 0; glyph < m_glyph_count; ++glyph) { 112 if (get_outer_rect(glyph).contains(event.position())) { 113 set_selected_glyph(glyph); 114 break; 115 } 116 } 117} 118 119void GlyphMapWidget::keydown_event(GUI::KeyEvent& event) 120{ 121 GUI::Frame::keydown_event(event); 122 123 if (event.key() == KeyCode::Key_Up) { 124 if (selected_glyph() >= m_columns) { 125 set_selected_glyph(selected_glyph() - m_columns); 126 return; 127 } 128 } 129 if (event.key() == KeyCode::Key_Down) { 130 if (selected_glyph() < m_glyph_count - m_columns) { 131 set_selected_glyph(selected_glyph() + m_columns); 132 return; 133 } 134 } 135 if (event.key() == KeyCode::Key_Left) { 136 if (selected_glyph() > 0) { 137 set_selected_glyph(selected_glyph() - 1); 138 return; 139 } 140 } 141 if (event.key() == KeyCode::Key_Right) { 142 if (selected_glyph() < m_glyph_count - 1) { 143 set_selected_glyph(selected_glyph() + 1); 144 return; 145 } 146 } 147 if (event.ctrl() && event.key() == KeyCode::Key_Home) { 148 set_selected_glyph(0); 149 return; 150 } 151 if (event.ctrl() && event.key() == KeyCode::Key_End) { 152 set_selected_glyph(m_glyph_count - 1); 153 return; 154 } 155 if (!event.ctrl() && event.key() == KeyCode::Key_Home) { 156 set_selected_glyph(selected_glyph() / m_columns * m_columns); 157 return; 158 } 159 if (!event.ctrl() && event.key() == KeyCode::Key_End) { 160 int new_selection = selected_glyph() / m_columns * m_columns + (m_columns - 1); 161 new_selection = clamp(new_selection, 0, m_glyph_count - 1); 162 set_selected_glyph(new_selection); 163 return; 164 } 165}