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