Serenity Operating System
at hosted 100 lines 3.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#pragma once 28 29#include <LibGUI/Widget.h> 30 31namespace GUI { 32 33class AbstractButton : public Widget { 34 C_OBJECT_ABSTRACT(AbstractButton) 35public: 36 virtual ~AbstractButton() override; 37 38 Function<void(bool)> on_checked; 39 40 void set_text(const StringView&); 41 const String& text() const { return m_text; } 42 43 bool is_exclusive() const { return m_exclusive; } 44 void set_exclusive(bool b) { m_exclusive = b; } 45 46 bool is_checked() const { return m_checked; } 47 void set_checked(bool); 48 49 bool is_checkable() const { return m_checkable; } 50 void set_checkable(bool); 51 52 bool is_hovered() const { return m_hovered; } 53 bool is_being_pressed() const { return m_being_pressed; } 54 55 virtual void click() = 0; 56 virtual bool accepts_focus() const override { return true; } 57 virtual bool is_uncheckable() const { return true; } 58 59 int auto_repeat_interval() const { return m_auto_repeat_interval; } 60 void set_auto_repeat_interval(int interval) { m_auto_repeat_interval = interval; } 61 62protected: 63 explicit AbstractButton(const StringView& = {}); 64 65 virtual void mousedown_event(MouseEvent&) override; 66 virtual void mousemove_event(MouseEvent&) override; 67 virtual void mouseup_event(MouseEvent&) override; 68 virtual void keydown_event(KeyEvent&) override; 69 virtual void enter_event(Core::Event&) override; 70 virtual void leave_event(Core::Event&) override; 71 virtual void change_event(Event&) override; 72 73 virtual void save_to(JsonObject&) override; 74 virtual bool set_property(const StringView& name, const JsonValue& value) override; 75 76 void paint_text(Painter&, const Gfx::Rect&, const Gfx::Font&, Gfx::TextAlignment); 77 78private: 79 virtual bool is_abstract_button() const final { return true; } 80 81 String m_text; 82 bool m_checked { false }; 83 bool m_checkable { false }; 84 bool m_hovered { false }; 85 bool m_being_pressed { false }; 86 bool m_exclusive { false }; 87 88 int m_auto_repeat_interval { 0 }; 89 RefPtr<Core::Timer> m_auto_repeat_timer; 90}; 91 92} 93 94template<> 95inline bool Core::is<GUI::AbstractButton>(const Core::Object& object) 96{ 97 if (!is<GUI::Widget>(object)) 98 return false; 99 return to<GUI::Widget>(object).is_abstract_button(); 100}