main.cpp 7.9 KB

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