Serenity Operating System
at portability 109 lines 3.9 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 <LibGfx/CharacterBitmap.h> 28#include <LibGfx/Painter.h> 29#include <LibGfx/StylePainter.h> 30#include <WindowServer/Button.h> 31#include <WindowServer/Event.h> 32#include <WindowServer/WindowManager.h> 33 34namespace WindowServer { 35 36Button::Button(WindowFrame& frame, NonnullRefPtr<Gfx::CharacterBitmap>&& bitmap, Function<void(Button&)>&& on_click_handler) 37 : on_click(move(on_click_handler)) 38 , m_frame(frame) 39 , m_bitmap(move(bitmap)) 40{ 41} 42 43Button::~Button() 44{ 45} 46 47void Button::paint(Gfx::Painter& painter) 48{ 49 auto palette = WindowManager::the().palette(); 50 Gfx::PainterStateSaver saver(painter); 51 painter.translate(relative_rect().location()); 52 Gfx::StylePainter::paint_button(painter, rect(), palette, Gfx::ButtonStyle::Normal, m_pressed, m_hovered); 53 auto x_location = rect().center(); 54 x_location.move_by(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2)); 55 if (m_pressed) 56 x_location.move_by(1, 1); 57 painter.draw_bitmap(x_location, *m_bitmap, palette.button_text()); 58} 59 60void Button::on_mouse_event(const MouseEvent& event) 61{ 62 auto& wm = WindowManager::the(); 63 64 if (event.type() == Event::MouseDown && event.button() == MouseButton::Left) { 65 m_pressed = true; 66 wm.set_cursor_tracking_button(this); 67 wm.invalidate(screen_rect()); 68 return; 69 } 70 71 if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) { 72 if (wm.cursor_tracking_button() != this) 73 return; 74 wm.set_cursor_tracking_button(nullptr); 75 bool old_pressed = m_pressed; 76 m_pressed = false; 77 if (rect().contains(event.position())) { 78 if (on_click) 79 on_click(*this); 80 } 81 if (old_pressed != m_pressed) 82 wm.invalidate(screen_rect()); 83 return; 84 } 85 86 if (event.type() == Event::MouseMove) { 87 bool old_hovered = m_hovered; 88 m_hovered = rect().contains(event.position()); 89 wm.set_hovered_button(m_hovered ? this : nullptr); 90 if (old_hovered != m_hovered) 91 wm.invalidate(screen_rect()); 92 } 93 94 if (event.type() == Event::MouseMove && event.buttons() & (unsigned)MouseButton::Left) { 95 if (wm.cursor_tracking_button() != this) 96 return; 97 bool old_pressed = m_pressed; 98 m_pressed = m_hovered; 99 if (old_pressed != m_pressed) 100 wm.invalidate(screen_rect()); 101 } 102} 103 104Gfx::Rect Button::screen_rect() const 105{ 106 return m_relative_rect.translated(m_frame.rect().location()); 107} 108 109}