VBWidgetRegistry.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "VBWidgetRegistry.h"
  27. #include "VBProperty.h"
  28. #include <LibGUI/GButton.h>
  29. #include <LibGUI/GCheckBox.h>
  30. #include <LibGUI/GGroupBox.h>
  31. #include <LibGUI/GLabel.h>
  32. #include <LibGUI/GProgressBar.h>
  33. #include <LibGUI/GRadioButton.h>
  34. #include <LibGUI/GScrollBar.h>
  35. #include <LibGUI/GSlider.h>
  36. #include <LibGUI/GSpinBox.h>
  37. #include <LibGUI/GTextEditor.h>
  38. String to_class_name(VBWidgetType type)
  39. {
  40. switch (type) {
  41. case VBWidgetType::GWidget:
  42. return "GUI::Widget";
  43. case VBWidgetType::GButton:
  44. return "GButton";
  45. case VBWidgetType::GLabel:
  46. return "GLabel";
  47. case VBWidgetType::GSpinBox:
  48. return "GSpinBox";
  49. case VBWidgetType::GTextEditor:
  50. return "GTextEditor";
  51. case VBWidgetType::GProgressBar:
  52. return "GProgressBar";
  53. case VBWidgetType::GCheckBox:
  54. return "GCheckBox";
  55. case VBWidgetType::GRadioButton:
  56. return "GRadioButton";
  57. case VBWidgetType::GScrollBar:
  58. return "GScrollBar";
  59. case VBWidgetType::GGroupBox:
  60. return "GGroupBox";
  61. case VBWidgetType::GSlider:
  62. return "GSlider";
  63. default:
  64. ASSERT_NOT_REACHED();
  65. }
  66. }
  67. VBWidgetType widget_type_from_class_name(const StringView& name)
  68. {
  69. if (name == "GUI::Widget")
  70. return VBWidgetType::GWidget;
  71. if (name == "GButton")
  72. return VBWidgetType::GButton;
  73. if (name == "GLabel")
  74. return VBWidgetType::GLabel;
  75. if (name == "GSpinBox")
  76. return VBWidgetType::GSpinBox;
  77. if (name == "GTextEditor")
  78. return VBWidgetType::GTextEditor;
  79. if (name == "GProgressBar")
  80. return VBWidgetType::GProgressBar;
  81. if (name == "GCheckBox")
  82. return VBWidgetType::GCheckBox;
  83. if (name == "GRadioButton")
  84. return VBWidgetType::GRadioButton;
  85. if (name == "GScrollBar")
  86. return VBWidgetType::GScrollBar;
  87. if (name == "GGroupBox")
  88. return VBWidgetType::GGroupBox;
  89. if (name == "GSlider")
  90. return VBWidgetType::GSlider;
  91. ASSERT_NOT_REACHED();
  92. }
  93. static RefPtr<GUI::Widget> build_gwidget(VBWidgetType type, GUI::Widget* parent)
  94. {
  95. switch (type) {
  96. case VBWidgetType::GWidget:
  97. return GUI::Widget::construct(parent);
  98. case VBWidgetType::GScrollBar:
  99. return GUI::ScrollBar::construct(Orientation::Vertical, parent);
  100. case VBWidgetType::GGroupBox:
  101. return GUI::GroupBox::construct("groupbox_1", parent);
  102. case VBWidgetType::GLabel: {
  103. auto label = GUI::Label::construct(parent);
  104. label->set_fill_with_background_color(true);
  105. label->set_text("label_1");
  106. return label;
  107. }
  108. case VBWidgetType::GButton: {
  109. auto button = GUI::Button::construct(parent);
  110. button->set_text("button_1");
  111. return button;
  112. }
  113. case VBWidgetType::GSpinBox: {
  114. auto box = GUI::SpinBox::construct(parent);
  115. box->set_range(0, 100);
  116. box->set_value(0);
  117. return box;
  118. }
  119. case VBWidgetType::GTextEditor: {
  120. auto editor = GUI::TextEditor::construct(GUI::TextEditor::Type::MultiLine, parent);
  121. editor->set_ruler_visible(false);
  122. return editor;
  123. }
  124. case VBWidgetType::GProgressBar: {
  125. auto bar = GUI::ProgressBar::construct(parent);
  126. bar->set_format(GUI::ProgressBar::Format::NoText);
  127. bar->set_range(0, 100);
  128. bar->set_value(50);
  129. return bar;
  130. }
  131. case VBWidgetType::GSlider: {
  132. auto slider = GUI::HorizontalSlider::construct(parent);
  133. slider->set_range(0, 100);
  134. slider->set_value(50);
  135. return slider;
  136. }
  137. case VBWidgetType::GCheckBox: {
  138. auto box = GUI::CheckBox::construct(parent);
  139. box->set_text("checkbox_1");
  140. return box;
  141. }
  142. case VBWidgetType::GRadioButton:
  143. return GUI::RadioButton::construct("radio_1", parent);
  144. default:
  145. ASSERT_NOT_REACHED();
  146. return nullptr;
  147. }
  148. }
  149. RefPtr<GUI::Widget> VBWidgetRegistry::build_gwidget(VBWidget& widget, VBWidgetType type, GUI::Widget* parent, NonnullOwnPtrVector<VBProperty>& properties)
  150. {
  151. auto gwidget = ::build_gwidget(type, parent);
  152. auto add_readonly_property = [&](const String& name, const GUI::Variant& value) {
  153. auto property = make<VBProperty>(widget, name, value);
  154. property->set_readonly(true);
  155. properties.append(move(property));
  156. };
  157. add_readonly_property("class", to_class_name(type));
  158. return gwidget;
  159. }