main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CalculatorWidget.h"
  7. #include <LibCore/System.h>
  8. #include <LibCrypto/NumberTheory/ModularFunctions.h>
  9. #include <LibGUI/Action.h>
  10. #include <LibGUI/ActionGroup.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/Clipboard.h>
  13. #include <LibGUI/Icon.h>
  14. #include <LibGUI/InputBox.h>
  15. #include <LibGUI/Menu.h>
  16. #include <LibGUI/Menubar.h>
  17. #include <LibGUI/Window.h>
  18. #include <LibGfx/Bitmap.h>
  19. #include <LibMain/Main.h>
  20. ErrorOr<int> serenity_main(Main::Arguments arguments)
  21. {
  22. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  23. auto app = TRY(GUI::Application::create(arguments));
  24. TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
  25. TRY(Core::System::unveil("/res", "r"));
  26. TRY(Core::System::unveil(nullptr, nullptr));
  27. auto app_icon = GUI::Icon::default_icon("app-calculator"sv);
  28. auto window = GUI::Window::construct();
  29. window->set_title("Calculator");
  30. window->set_resizable(false);
  31. window->resize(250, 215);
  32. auto widget = TRY(window->set_main_widget<CalculatorWidget>());
  33. window->set_icon(app_icon.bitmap_for_size(16));
  34. auto file_menu = window->add_menu("&File"_string);
  35. file_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  36. GUI::Application::the()->quit();
  37. }));
  38. auto edit_menu = window->add_menu("&Edit"_string);
  39. edit_menu->add_action(GUI::CommonActions::make_copy_action([&](auto&) {
  40. GUI::Clipboard::the().set_plain_text(widget->get_entry());
  41. }));
  42. edit_menu->add_action(GUI::CommonActions::make_paste_action([&](auto&) {
  43. auto clipboard = GUI::Clipboard::the().fetch_data_and_type();
  44. if (clipboard.mime_type == "text/plain") {
  45. if (!clipboard.data.is_empty()) {
  46. auto const number = StringView(clipboard.data);
  47. widget->set_typed_entry(Crypto::BigFraction(number));
  48. }
  49. }
  50. }));
  51. auto constants_menu = window->add_menu("&Constants"_string);
  52. auto const power = Crypto::NumberTheory::Power("10"_bigint, "10"_bigint);
  53. constants_menu->add_action(GUI::Action::create("&Pi", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/pi.png"sv)), [&](auto&) {
  54. widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(31415926535), power });
  55. }));
  56. constants_menu->add_action(GUI::Action::create("&Euler's Number", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/eulers_number.png"sv)), [&](auto&) {
  57. widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(27182818284), power });
  58. }));
  59. constants_menu->add_action(GUI::Action::create("&Phi", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/phi.png"sv)), [&](auto&) {
  60. widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(16180339887), power });
  61. }));
  62. auto round_menu = window->add_menu("&Round"_string);
  63. GUI::ActionGroup preview_actions;
  64. static constexpr auto rounding_modes = Array { 0, 2, 4 };
  65. Optional<unsigned> last_rounding_mode = 1;
  66. for (unsigned i {}; i < rounding_modes.size(); ++i) {
  67. auto round_action = GUI::Action::create_checkable(DeprecatedString::formatted("To &{} Digits", rounding_modes[i]),
  68. [&widget, rounding_mode = rounding_modes[i], &last_rounding_mode, i](auto&) {
  69. widget->set_rounding_length(rounding_mode);
  70. last_rounding_mode = i;
  71. });
  72. preview_actions.add_action(*round_action);
  73. round_menu->add_action(*round_action);
  74. }
  75. constexpr auto format { "&Custom - {}..."sv };
  76. auto round_custom = GUI::Action::create_checkable(DeprecatedString::formatted(format, 0), [&](auto& action) {
  77. int custom_rounding_length = widget->rounding_length();
  78. auto result = GUI::InputBox::show_numeric(window, custom_rounding_length, 0, 100, "Digits to Round"sv);
  79. if (!result.is_error() && result.value() == GUI::Dialog::ExecResult::OK) {
  80. action.set_text(DeprecatedString::formatted(format, custom_rounding_length));
  81. widget->set_rounding_length(custom_rounding_length);
  82. last_rounding_mode.clear();
  83. } else if (last_rounding_mode.has_value())
  84. round_menu->action_at(last_rounding_mode.value())
  85. ->activate();
  86. });
  87. widget->set_rounding_custom(round_custom, format);
  88. auto shrink_action = GUI::Action::create("&Shrink...", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-cut.png"sv)), [&](auto&) {
  89. int shrink_length = widget->rounding_length();
  90. auto result = GUI::InputBox::show_numeric(window, shrink_length, 0, 100, "Digits to Shrink"sv);
  91. if (!result.is_error() && result.value() == GUI::Dialog::ExecResult::OK) {
  92. round_custom->set_checked(true);
  93. round_custom->set_text(DeprecatedString::formatted(format, shrink_length));
  94. widget->set_rounding_length(shrink_length);
  95. widget->shrink(shrink_length);
  96. }
  97. });
  98. preview_actions.add_action(*round_custom);
  99. preview_actions.set_exclusive(true);
  100. round_menu->add_action(*round_custom);
  101. round_menu->add_action(*shrink_action);
  102. round_menu->action_at(last_rounding_mode.value())->activate();
  103. auto help_menu = window->add_menu("&Help"_string);
  104. help_menu->add_action(GUI::CommonActions::make_command_palette_action(window));
  105. help_menu->add_action(GUI::CommonActions::make_about_action("Calculator"_string, app_icon, window));
  106. window->show();
  107. return app->exec();
  108. }