main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "PaintableWidget.h"
  27. #include "PaletteWidget.h"
  28. #include "ToolboxWidget.h"
  29. #include <LibGUI/AboutDialog.h>
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/FilePicker.h>
  34. #include <LibGUI/Menu.h>
  35. #include <LibGUI/MenuBar.h>
  36. #include <LibGUI/MessageBox.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibGfx/Bitmap.h>
  39. #include <stdio.h>
  40. int main(int argc, char** argv)
  41. {
  42. if (pledge("stdio thread shared_buffer accept rpath unix wpath cpath fattr", nullptr) < 0) {
  43. perror("pledge");
  44. return 1;
  45. }
  46. GUI::Application app(argc, argv);
  47. if (pledge("stdio thread shared_buffer accept rpath wpath cpath", nullptr) < 0) {
  48. perror("pledge");
  49. return 1;
  50. }
  51. auto window = GUI::Window::construct();
  52. window->set_title("PaintBrush");
  53. window->set_rect(100, 100, 640, 480);
  54. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-paintbrush.png"));
  55. auto& horizontal_container = window->set_main_widget<GUI::Widget>();
  56. horizontal_container.set_layout<GUI::HorizontalBoxLayout>();
  57. horizontal_container.layout()->set_spacing(0);
  58. horizontal_container.add<ToolboxWidget>();
  59. auto vertical_container = horizontal_container.add<GUI::Widget>();
  60. vertical_container->set_layout<GUI::VerticalBoxLayout>();
  61. vertical_container->layout()->set_spacing(0);
  62. auto paintable_widget = vertical_container->add<PaintableWidget>();
  63. paintable_widget->set_focus(true);
  64. vertical_container->add<PaletteWidget>(*paintable_widget);
  65. window->show();
  66. auto menubar = make<GUI::MenuBar>();
  67. auto app_menu = GUI::Menu::construct("PaintBrush");
  68. app_menu->add_action(GUI::CommonActions::make_open_action([&](auto&) {
  69. Optional<String> open_path = GUI::FilePicker::get_open_filepath();
  70. if (!open_path.has_value())
  71. return;
  72. auto bitmap = Gfx::Bitmap::load_from_file(open_path.value());
  73. if (!bitmap) {
  74. GUI::MessageBox::show(String::format("Failed to load '%s'", open_path.value().characters()), "Open failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
  75. return;
  76. }
  77. paintable_widget->set_bitmap(*bitmap);
  78. }));
  79. app_menu->add_separator();
  80. app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  81. GUI::Application::the().quit(0);
  82. return;
  83. }));
  84. menubar->add_menu(move(app_menu));
  85. auto edit_menu = GUI::Menu::construct("Edit");
  86. menubar->add_menu(move(edit_menu));
  87. auto help_menu = GUI::Menu::construct("Help");
  88. help_menu->add_action(GUI::Action::create("About", [&](auto&) {
  89. GUI::AboutDialog::show("PaintBrush", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-paintbrush.png"), window);
  90. }));
  91. menubar->add_menu(move(help_menu));
  92. app.set_menubar(move(menubar));
  93. return app.exec();
  94. }