Serenity Operating System
at hosted 244 lines 9.3 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 "VBWidget.h" 28#include "VBForm.h" 29#include "VBProperty.h" 30#include "VBWidgetPropertyModel.h" 31#include "VBWidgetRegistry.h" 32#include <LibGUI/Button.h> 33#include <LibGUI/CheckBox.h> 34#include <LibGUI/GroupBox.h> 35#include <LibGUI/Label.h> 36#include <LibGUI/Painter.h> 37#include <LibGUI/ProgressBar.h> 38#include <LibGUI/RadioButton.h> 39#include <LibGUI/ScrollBar.h> 40#include <LibGUI/Slider.h> 41#include <LibGUI/SpinBox.h> 42#include <LibGUI/TextEditor.h> 43 44VBWidget::VBWidget(VBWidgetType type, VBForm& form, VBWidget* parent) 45 : m_type(type) 46 , m_form(form) 47 , m_property_model(VBWidgetPropertyModel::create(*this)) 48{ 49 auto* widget_parent = parent ? parent->gwidget() : &form; 50 m_gwidget = VBWidgetRegistry::build_gwidget(*this, type, widget_parent, m_properties); 51 m_form.m_gwidget_map.set(m_gwidget, this); 52 setup_properties(); 53} 54 55VBWidget::~VBWidget() 56{ 57 m_form.m_gwidget_map.remove(m_gwidget); 58 m_form.m_selected_widgets.remove(this); 59 m_gwidget->parent()->remove_child(*m_gwidget); 60} 61 62Gfx::Rect VBWidget::rect() const 63{ 64 return m_gwidget->window_relative_rect(); 65} 66 67void VBWidget::set_rect(const Gfx::Rect& rect) 68{ 69 if (rect == m_gwidget->window_relative_rect()) 70 return; 71 auto new_window_relative_rect = rect; 72 if (m_gwidget->parent()) 73 new_window_relative_rect.move_by(-m_gwidget->parent_widget()->window_relative_rect().location()); 74 m_gwidget->set_relative_rect(new_window_relative_rect); 75 synchronize_properties(); 76} 77 78bool VBWidget::is_selected() const 79{ 80 return m_form.is_selected(*this); 81} 82 83Gfx::Rect VBWidget::grabber_rect(Direction direction) const 84{ 85 int grabber_size = 5; 86 int half_grabber_size = grabber_size / 2; 87 switch (direction) { 88 case Direction::Left: 89 return { rect().x() - half_grabber_size, rect().center().y() - half_grabber_size, grabber_size, grabber_size }; 90 case Direction::UpLeft: 91 return { rect().x() - half_grabber_size, rect().y() - half_grabber_size, grabber_size, grabber_size }; 92 case Direction::Up: 93 return { rect().center().x() - half_grabber_size, rect().y() - half_grabber_size, grabber_size, grabber_size }; 94 case Direction::UpRight: 95 return { rect().right() - half_grabber_size, rect().y() - half_grabber_size, grabber_size, grabber_size }; 96 case Direction::Right: 97 return { rect().right() - half_grabber_size, rect().center().y() - half_grabber_size, grabber_size, grabber_size }; 98 case Direction::DownLeft: 99 return { rect().x() - half_grabber_size, rect().bottom() - half_grabber_size, grabber_size, grabber_size }; 100 case Direction::Down: 101 return { rect().center().x() - half_grabber_size, rect().bottom() - half_grabber_size, grabber_size, grabber_size }; 102 case Direction::DownRight: 103 return { rect().right() - half_grabber_size, rect().bottom() - half_grabber_size, grabber_size, grabber_size }; 104 default: 105 ASSERT_NOT_REACHED(); 106 } 107} 108 109Direction VBWidget::grabber_at(const Gfx::Point& position) const 110{ 111 Direction found_grabber = Direction::None; 112 for_each_direction([&](Direction direction) { 113 if (grabber_rect(direction).contains(position)) 114 found_grabber = direction; 115 }); 116 return found_grabber; 117} 118 119void VBWidget::for_each_property(Function<void(VBProperty&)> callback) 120{ 121 for (auto& it : m_properties) { 122 callback(it); 123 } 124} 125 126void VBWidget::add_property(const String& name, Function<GUI::Variant(const GUI::Widget&)>&& getter, Function<void(GUI::Widget&, const GUI::Variant&)>&& setter) 127{ 128 auto& prop = property(name); 129 prop.m_getter = move(getter); 130 prop.m_setter = move(setter); 131} 132 133#define VB_ADD_PROPERTY(gclass, name, getter, setter, variant_type) \ 134 add_property( \ 135 name, \ 136 [](auto& widget) -> GUI::Variant { return ((const gclass&)widget).getter(); }, \ 137 [](auto& widget, auto& value) { ((gclass&)widget).setter(value.to_##variant_type()); }) 138 139void VBWidget::setup_properties() 140{ 141 VB_ADD_PROPERTY(Core::Object, "name", name, set_name, string); 142 143 VB_ADD_PROPERTY(GUI::Widget, "width", width, set_width, i32); 144 VB_ADD_PROPERTY(GUI::Widget, "height", height, set_height, i32); 145 VB_ADD_PROPERTY(GUI::Widget, "x", x, set_x, i32); 146 VB_ADD_PROPERTY(GUI::Widget, "y", y, set_y, i32); 147 VB_ADD_PROPERTY(GUI::Widget, "visible", is_visible, set_visible, bool); 148 VB_ADD_PROPERTY(GUI::Widget, "enabled", is_enabled, set_enabled, bool); 149 VB_ADD_PROPERTY(GUI::Widget, "tooltip", tooltip, set_tooltip, string); 150 VB_ADD_PROPERTY(GUI::Widget, "backcolor", background_color, set_background_color, color); 151 VB_ADD_PROPERTY(GUI::Widget, "forecolor", foreground_color, set_foreground_color, color); 152 VB_ADD_PROPERTY(GUI::Widget, "autofill", fill_with_background_color, set_fill_with_background_color, bool); 153 154 if (m_type == VBWidgetType::GLabel) { 155 VB_ADD_PROPERTY(GUI::Label, "text", text, set_text, string); 156 } 157 158 if (m_type == VBWidgetType::GButton) { 159 VB_ADD_PROPERTY(GUI::Button, "text", text, set_text, string); 160 } 161 162 if (m_type == VBWidgetType::GGroupBox) { 163 VB_ADD_PROPERTY(GUI::GroupBox, "title", title, set_title, string); 164 } 165 166 if (m_type == VBWidgetType::GScrollBar) { 167 VB_ADD_PROPERTY(GUI::ScrollBar, "min", min, set_min, i32); 168 VB_ADD_PROPERTY(GUI::ScrollBar, "max", max, set_max, i32); 169 VB_ADD_PROPERTY(GUI::ScrollBar, "value", value, set_value, i32); 170 VB_ADD_PROPERTY(GUI::ScrollBar, "step", step, set_step, i32); 171 } 172 173 if (m_type == VBWidgetType::GSpinBox) { 174 VB_ADD_PROPERTY(GUI::SpinBox, "min", min, set_min, i32); 175 VB_ADD_PROPERTY(GUI::SpinBox, "max", max, set_max, i32); 176 VB_ADD_PROPERTY(GUI::SpinBox, "value", value, set_value, i32); 177 } 178 179 if (m_type == VBWidgetType::GProgressBar) { 180 VB_ADD_PROPERTY(GUI::ProgressBar, "min", min, set_min, i32); 181 VB_ADD_PROPERTY(GUI::ProgressBar, "max", max, set_max, i32); 182 VB_ADD_PROPERTY(GUI::ProgressBar, "value", value, set_value, i32); 183 } 184 185 if (m_type == VBWidgetType::GSlider) { 186 VB_ADD_PROPERTY(GUI::Slider, "min", min, set_min, i32); 187 VB_ADD_PROPERTY(GUI::Slider, "max", max, set_max, i32); 188 VB_ADD_PROPERTY(GUI::Slider, "value", value, set_value, i32); 189 } 190 191 if (m_type == VBWidgetType::GTextEditor) { 192 VB_ADD_PROPERTY(GUI::TextEditor, "text", text, set_text, string); 193 VB_ADD_PROPERTY(GUI::TextEditor, "ruler_visible", is_ruler_visible, set_ruler_visible, bool); 194 } 195 196 if (m_type == VBWidgetType::GCheckBox) { 197 VB_ADD_PROPERTY(GUI::CheckBox, "text", text, set_text, string); 198 VB_ADD_PROPERTY(GUI::CheckBox, "checked", is_checked, set_checked, bool); 199 } 200 201 if (m_type == VBWidgetType::GRadioButton) { 202 VB_ADD_PROPERTY(GUI::RadioButton, "text", text, set_text, string); 203 VB_ADD_PROPERTY(GUI::RadioButton, "checked", is_checked, set_checked, bool); 204 } 205} 206 207void VBWidget::synchronize_properties() 208{ 209 for (auto& prop : m_properties) { 210 if (prop.m_getter) 211 prop.m_value = prop.m_getter(*gwidget()); 212 } 213 214 m_property_model->update(); 215} 216 217VBProperty& VBWidget::property(const String& name) 218{ 219 for (auto& prop : m_properties) { 220 if (prop.name() == name) 221 return prop; 222 } 223 m_properties.append(make<VBProperty>(*this, name, GUI::Variant())); 224 return m_properties.last(); 225} 226 227void VBWidget::property_did_change() 228{ 229 m_form.update(); 230} 231 232void VBWidget::capture_transform_origin_rect() 233{ 234 m_transform_origin_rect = rect(); 235} 236 237bool VBWidget::is_in_layout() const 238{ 239 if (auto* parent_widget = m_gwidget->parent_widget()) { 240 if (parent_widget->layout()) 241 return true; 242 } 243 return false; 244}