VBWidget.cpp 9.1 KB

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