main.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "TextEditorWidget.h"
  27. #include <LibCore/ArgsParser.h>
  28. #include <LibGUI/MenuBar.h>
  29. #include <LibGfx/Bitmap.h>
  30. #include <stdio.h>
  31. int main(int argc, char** argv)
  32. {
  33. if (pledge("stdio recvfd sendfd thread rpath accept cpath wpath unix fattr", nullptr) < 0) {
  34. perror("pledge");
  35. return 1;
  36. }
  37. auto app = GUI::Application::construct(argc, argv);
  38. if (pledge("stdio recvfd sendfd thread rpath accept cpath wpath unix", nullptr) < 0) {
  39. perror("pledge");
  40. return 1;
  41. }
  42. const char* preview_mode = "auto";
  43. const char* file_to_edit = nullptr;
  44. Core::ArgsParser parser;
  45. parser.add_option(preview_mode, "Preview mode, one of 'none', 'html', 'markdown', 'auto'", "preview-mode", '\0', "mode");
  46. parser.add_positional_argument(file_to_edit, "File to edit", "file", Core::ArgsParser::Required::No);
  47. parser.parse(argc, argv);
  48. StringView preview_mode_view = preview_mode;
  49. auto app_icon = GUI::Icon::default_icon("app-text-editor");
  50. auto window = GUI::Window::construct();
  51. window->resize(640, 400);
  52. auto& text_widget = window->set_main_widget<TextEditorWidget>();
  53. text_widget.editor().set_focus(true);
  54. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  55. if (text_widget.request_close())
  56. return GUI::Window::CloseRequestDecision::Close;
  57. return GUI::Window::CloseRequestDecision::StayOpen;
  58. };
  59. if (preview_mode_view == "auto") {
  60. text_widget.set_auto_detect_preview_mode(true);
  61. } else if (preview_mode_view == "markdown") {
  62. text_widget.set_preview_mode(TextEditorWidget::PreviewMode::Markdown);
  63. } else if (preview_mode_view == "html") {
  64. text_widget.set_preview_mode(TextEditorWidget::PreviewMode::HTML);
  65. } else if (preview_mode_view == "none") {
  66. text_widget.set_preview_mode(TextEditorWidget::PreviewMode::None);
  67. } else {
  68. warnln("Invalid mode '{}'", preview_mode);
  69. return 1;
  70. }
  71. if (file_to_edit)
  72. text_widget.open_file(file_to_edit);
  73. else
  74. text_widget.update_title();
  75. auto menubar = GUI::MenuBar::construct();
  76. text_widget.initialize_menubar(menubar);
  77. app->set_menubar(menubar);
  78. window->show();
  79. window->set_icon(app_icon.bitmap_for_size(16));
  80. return app->exec();
  81. }