2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2020-01-24 10:45:29 -03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-08-09 07:55:20 -03:00
|
|
|
#include "CalculatorWidget.h"
|
2021-11-23 19:45:55 -03:00
|
|
|
#include <LibCore/System.h>
|
2022-01-12 07:05:03 -03:00
|
|
|
#include <LibCrypto/NumberTheory/ModularFunctions.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/Application.h>
|
2021-02-24 11:35:39 -03:00
|
|
|
#include <LibGUI/Clipboard.h>
|
2020-11-02 16:30:17 -03:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-02-14 21:56:30 -03:00
|
|
|
#include <LibGUI/Menu.h>
|
2021-04-13 11:18:20 -03:00
|
|
|
#include <LibGUI/Menubar.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Window.h>
|
2020-02-14 19:02:47 -03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2021-11-23 19:45:55 -03:00
|
|
|
#include <LibMain/Main.h>
|
2020-01-13 08:22:49 -03:00
|
|
|
#include <stdio.h>
|
2021-03-12 13:29:37 -03:00
|
|
|
#include <unistd.h>
|
2019-08-09 07:55:20 -03:00
|
|
|
|
2021-11-23 19:45:55 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-08-09 07:55:20 -03:00
|
|
|
{
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
|
2021-11-23 19:45:55 -03:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2020-01-13 08:22:49 -03:00
|
|
|
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
|
2021-11-23 19:45:55 -03:00
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-01-21 08:03:08 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-calculator"sv);
|
2020-11-02 16:30:17 -03:00
|
|
|
|
2021-11-23 19:45:55 -03:00
|
|
|
auto window = TRY(GUI::Window::try_create());
|
2019-08-09 07:55:20 -03:00
|
|
|
window->set_title("Calculator");
|
|
|
|
|
window->set_resizable(false);
|
2021-07-27 16:11:06 -03:00
|
|
|
window->resize(250, 215);
|
2019-08-09 07:55:20 -03:00
|
|
|
|
2022-01-07 11:03:19 -03:00
|
|
|
auto widget = TRY(window->try_set_main_widget<CalculatorWidget>());
|
2019-08-09 07:55:20 -03:00
|
|
|
|
2020-11-02 16:30:17 -03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2019-12-30 19:12:52 -03:00
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
auto& file_menu = window->add_menu("&File");
|
2021-05-01 05:45:39 -03:00
|
|
|
file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
2020-07-04 11:52:01 -03:00
|
|
|
GUI::Application::the()->quit();
|
2019-12-30 19:12:52 -03:00
|
|
|
}));
|
|
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
auto& edit_menu = window->add_menu("&Edit");
|
2021-04-10 11:09:23 -03:00
|
|
|
edit_menu.add_action(GUI::CommonActions::make_copy_action([&](auto&) {
|
2022-01-07 11:03:19 -03:00
|
|
|
GUI::Clipboard::the().set_plain_text(widget->get_entry());
|
2021-02-24 11:35:39 -03:00
|
|
|
}));
|
2021-04-10 11:09:23 -03:00
|
|
|
edit_menu.add_action(GUI::CommonActions::make_paste_action([&](auto&) {
|
2021-11-20 11:22:01 -03:00
|
|
|
auto clipboard = GUI::Clipboard::the().fetch_data_and_type();
|
2021-02-24 11:35:39 -03:00
|
|
|
if (clipboard.mime_type == "text/plain") {
|
|
|
|
|
if (!clipboard.data.is_empty()) {
|
2022-01-12 07:05:03 -03:00
|
|
|
widget->set_entry(Crypto::BigFraction(StringView(clipboard.data)));
|
2021-02-24 11:35:39 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
2021-10-29 13:00:15 -03:00
|
|
|
auto& constants_menu = window->add_menu("&Constants");
|
2022-01-12 07:05:03 -03:00
|
|
|
auto const power = Crypto::NumberTheory::Power("10"_bigint, "10"_bigint);
|
|
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
constants_menu.add_action(GUI::Action::create("&Pi", TRY(Gfx::Bitmap::try_load_from_file("/res/icons/calculator/pi.png"sv)), [&](auto&) {
|
2022-01-12 07:05:03 -03:00
|
|
|
widget->set_entry(Crypto::BigFraction { Crypto::SignedBigInteger(31415926535), power });
|
2021-10-29 13:00:15 -03:00
|
|
|
}));
|
2022-07-11 14:32:29 -03:00
|
|
|
constants_menu.add_action(GUI::Action::create("&Euler's Number", TRY(Gfx::Bitmap::try_load_from_file("/res/icons/calculator/eulers_number.png"sv)), [&](auto&) {
|
2022-01-12 07:05:03 -03:00
|
|
|
widget->set_entry(Crypto::BigFraction { Crypto::SignedBigInteger(27182818284), power });
|
2021-10-29 13:00:15 -03:00
|
|
|
}));
|
2022-07-11 14:32:29 -03:00
|
|
|
constants_menu.add_action(GUI::Action::create("&Phi", TRY(Gfx::Bitmap::try_load_from_file("/res/icons/calculator/phi.png"sv)), [&](auto&) {
|
2022-01-12 07:05:03 -03:00
|
|
|
widget->set_entry(Crypto::BigFraction { Crypto::SignedBigInteger(16180339887), power });
|
2022-06-09 14:12:56 -03:00
|
|
|
}));
|
2021-10-29 13:00:15 -03:00
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
auto& help_menu = window->add_menu("&Help");
|
2021-01-30 09:38:19 -03:00
|
|
|
help_menu.add_action(GUI::CommonActions::make_about_action("Calculator", app_icon, window));
|
2021-07-28 07:32:29 -03:00
|
|
|
|
|
|
|
|
window->show();
|
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
return app->exec();
|
2019-08-09 07:55:20 -03:00
|
|
|
}
|