main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "VBForm.h"
  27. #include "VBPropertiesWindow.h"
  28. #include "VBWidget.h"
  29. #include "VBWidgetPropertyModel.h"
  30. #include <LibGUI/AboutDialog.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/BoxLayout.h>
  34. #include <LibGUI/Button.h>
  35. #include <LibGUI/Menu.h>
  36. #include <LibGUI/MenuBar.h>
  37. #include <LibGUI/TableView.h>
  38. #include <LibGUI/Widget.h>
  39. #include <LibGUI/Window.h>
  40. #include <fcntl.h>
  41. #include <signal.h>
  42. #include <stdio.h>
  43. #include <unistd.h>
  44. static RefPtr<GUI::Window> make_toolbox_window();
  45. int main(int argc, char** argv)
  46. {
  47. auto app = GUI::Application::construct(argc, argv);
  48. auto propbox = VBPropertiesWindow::construct();
  49. auto form1 = VBForm::construct("Form1");
  50. form1->on_widget_selected = [&](VBWidget* widget) {
  51. propbox->table_view().set_model(widget ? &widget->property_model() : nullptr);
  52. };
  53. auto menubar = GUI::MenuBar::construct();
  54. auto& app_menu = menubar->add_menu("Visual Builder");
  55. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  56. GUI::Application::the()->quit();
  57. return;
  58. }));
  59. auto& file_menu = menubar->add_menu("File");
  60. file_menu.add_action(GUI::Action::create("Dump Form", [&](auto&) {
  61. form1->dump();
  62. }));
  63. file_menu.add_action(GUI::Action::create("Save Form...", { Mod_Ctrl, Key_S }, [&](auto&) {
  64. form1->write_to_file("/tmp/form.frm");
  65. }));
  66. auto window = GUI::Window::construct();
  67. window->set_title(form1->name());
  68. window->set_rect(120, 200, 640, 400);
  69. window->set_main_widget(form1);
  70. window->show();
  71. auto& help_menu = menubar->add_menu("Help");
  72. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  73. GUI::AboutDialog::show("Visual Builder", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-visual-builder.png"), window);
  74. }));
  75. app->set_menubar(move(menubar));
  76. auto toolbox = make_toolbox_window();
  77. toolbox->show();
  78. propbox->show();
  79. if (argc == 2) {
  80. form1->load_from_file(argv[1]);
  81. }
  82. return app->exec();
  83. }
  84. RefPtr<GUI::Window> make_toolbox_window()
  85. {
  86. auto window = GUI::Window::construct();
  87. window->set_title("Widgets");
  88. window->set_rect(20, 200, 80, 300);
  89. auto& widget = window->set_main_widget<GUI::Widget>();
  90. widget.set_fill_with_background_color(true);
  91. widget.set_layout<GUI::VerticalBoxLayout>();
  92. widget.layout()->set_spacing(0);
  93. auto& label_button = widget.add<GUI::Button>();
  94. label_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  95. label_button.set_tooltip("GLabel");
  96. label_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/label.png"));
  97. label_button.on_click = [](auto) {
  98. if (auto* form = VBForm::current())
  99. form->insert_widget(VBWidgetType::GLabel);
  100. };
  101. auto& button_button = widget.add<GUI::Button>();
  102. button_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  103. button_button.set_tooltip("GButton");
  104. button_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/button.png"));
  105. button_button.on_click = [](auto) {
  106. if (auto* form = VBForm::current())
  107. form->insert_widget(VBWidgetType::GButton);
  108. };
  109. auto& spinbox_button = widget.add<GUI::Button>();
  110. spinbox_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  111. spinbox_button.set_tooltip("GSpinBox");
  112. spinbox_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/spinbox.png"));
  113. spinbox_button.on_click = [](auto) {
  114. if (auto* form = VBForm::current())
  115. form->insert_widget(VBWidgetType::GSpinBox);
  116. };
  117. auto& editor_button = widget.add<GUI::Button>();
  118. editor_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  119. editor_button.set_tooltip("GTextEditor");
  120. editor_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/textbox.png"));
  121. editor_button.on_click = [](auto) {
  122. if (auto* form = VBForm::current())
  123. form->insert_widget(VBWidgetType::GTextEditor);
  124. };
  125. auto& progress_bar_button = widget.add<GUI::Button>();
  126. progress_bar_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  127. progress_bar_button.set_tooltip("GProgressBar");
  128. progress_bar_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/progressbar.png"));
  129. progress_bar_button.on_click = [](auto) {
  130. if (auto* form = VBForm::current())
  131. form->insert_widget(VBWidgetType::GProgressBar);
  132. };
  133. auto& slider_button = widget.add<GUI::Button>();
  134. slider_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  135. slider_button.set_tooltip("GSlider");
  136. slider_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/slider.png"));
  137. slider_button.on_click = [](auto) {
  138. if (auto* form = VBForm::current())
  139. form->insert_widget(VBWidgetType::GSlider);
  140. };
  141. auto& checkbox_button = widget.add<GUI::Button>();
  142. checkbox_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  143. checkbox_button.set_tooltip("GCheckBox");
  144. checkbox_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/checkbox.png"));
  145. checkbox_button.on_click = [](auto) {
  146. if (auto* form = VBForm::current())
  147. form->insert_widget(VBWidgetType::GCheckBox);
  148. };
  149. auto& radiobutton_button = widget.add<GUI::Button>();
  150. radiobutton_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  151. radiobutton_button.set_tooltip("GRadioButton");
  152. radiobutton_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/serenity/filled-radio-circle.png"));
  153. radiobutton_button.on_click = [](auto) {
  154. if (auto* form = VBForm::current())
  155. form->insert_widget(VBWidgetType::GRadioButton);
  156. };
  157. auto& scrollbar_button = widget.add<GUI::Button>();
  158. scrollbar_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  159. scrollbar_button.set_tooltip("GScrollBar");
  160. scrollbar_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/scrollbar.png"));
  161. scrollbar_button.on_click = [](auto) {
  162. if (auto* form = VBForm::current())
  163. form->insert_widget(VBWidgetType::GScrollBar);
  164. };
  165. auto& groupbox_button = widget.add<GUI::Button>();
  166. groupbox_button.set_button_style(Gfx::ButtonStyle::CoolBar);
  167. groupbox_button.set_tooltip("GGroupBox");
  168. groupbox_button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/vbwidgets/groupbox.png"));
  169. groupbox_button.on_click = [](auto) {
  170. if (auto* form = VBForm::current())
  171. form->insert_widget(VBWidgetType::GGroupBox);
  172. };
  173. return window;
  174. }