TextEditorWidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "TextEditorWidget.h"
  2. #include <AK/Optional.h>
  3. #include <AK/StringBuilder.h>
  4. #include <LibCore/CFile.h>
  5. #include <LibGUI/GAction.h>
  6. #include <LibGUI/GBoxLayout.h>
  7. #include <LibGUI/GFilePicker.h>
  8. #include <LibGUI/GFontDatabase.h>
  9. #include <LibGUI/GMenuBar.h>
  10. #include <LibGUI/GMessageBox.h>
  11. #include <LibGUI/GStatusBar.h>
  12. #include <LibGUI/GTextEditor.h>
  13. #include <LibGUI/GToolBar.h>
  14. TextEditorWidget::TextEditorWidget()
  15. {
  16. set_layout(make<GBoxLayout>(Orientation::Vertical));
  17. layout()->set_spacing(0);
  18. auto* toolbar = new GToolBar(this);
  19. m_editor = new GTextEditor(GTextEditor::MultiLine, this);
  20. m_editor->set_ruler_visible(true);
  21. m_editor->set_automatic_indentation_enabled(true);
  22. auto* statusbar = new GStatusBar(this);
  23. m_editor->on_cursor_change = [statusbar, this] {
  24. StringBuilder builder;
  25. builder.appendf("Line: %d, Column: %d", m_editor->cursor().line(), m_editor->cursor().column());
  26. statusbar->set_text(builder.to_string());
  27. };
  28. auto new_action = GAction::create("New document", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [](const GAction&) {
  29. dbgprintf("FIXME: Implement File/New\n");
  30. });
  31. auto open_action = GAction::create("Open document", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [this](const GAction&) {
  32. Optional<String> open_name = GFilePicker::get_open_filepath();
  33. if (!open_name.has_value())
  34. return;
  35. m_path = open_name.value();
  36. open_sesame(m_path);
  37. });
  38. auto save_action = GAction::create("Save document", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
  39. if (!m_path.is_empty()) {
  40. if (!m_editor->write_to_file(m_path))
  41. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, window());
  42. return;
  43. }
  44. Optional<String> save_name = GFilePicker::get_save_filepath();
  45. if (!save_name.has_value())
  46. return;
  47. if (!m_editor->write_to_file(save_name.value())) {
  48. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, window());
  49. return;
  50. }
  51. m_path = save_name.value();
  52. window()->set_title(String::format("Text Editor: %s", m_path.characters()));
  53. dbgprintf("Wrote document to '%s'\n", m_path.characters());
  54. });
  55. auto menubar = make<GMenuBar>();
  56. auto app_menu = make<GMenu>("Text Editor");
  57. app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) {
  58. GApplication::the().quit(0);
  59. return;
  60. }));
  61. menubar->add_menu(move(app_menu));
  62. auto file_menu = make<GMenu>("File");
  63. file_menu->add_action(new_action);
  64. file_menu->add_action(open_action);
  65. file_menu->add_action(save_action);
  66. menubar->add_menu(move(file_menu));
  67. auto edit_menu = make<GMenu>("Edit");
  68. edit_menu->add_action(m_editor->undo_action());
  69. edit_menu->add_action(m_editor->redo_action());
  70. edit_menu->add_separator();
  71. edit_menu->add_action(m_editor->cut_action());
  72. edit_menu->add_action(m_editor->copy_action());
  73. edit_menu->add_action(m_editor->paste_action());
  74. edit_menu->add_action(m_editor->delete_action());
  75. menubar->add_menu(move(edit_menu));
  76. auto font_menu = make<GMenu>("Font");
  77. GFontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
  78. font_menu->add_action(GAction::create(font_name, [this](const GAction& action) {
  79. m_editor->set_font(GFontDatabase::the().get_by_name(action.text()));
  80. m_editor->update();
  81. }));
  82. });
  83. menubar->add_menu(move(font_menu));
  84. auto help_menu = make<GMenu>("Help");
  85. help_menu->add_action(GAction::create("About", [](const GAction&) {
  86. dbgprintf("FIXME: Implement Help/About\n");
  87. }));
  88. menubar->add_menu(move(help_menu));
  89. GApplication::the().set_menubar(move(menubar));
  90. toolbar->add_action(move(new_action));
  91. toolbar->add_action(move(open_action));
  92. toolbar->add_action(move(save_action));
  93. toolbar->add_separator();
  94. toolbar->add_action(m_editor->cut_action());
  95. toolbar->add_action(m_editor->copy_action());
  96. toolbar->add_action(m_editor->paste_action());
  97. toolbar->add_action(m_editor->delete_action());
  98. toolbar->add_separator();
  99. toolbar->add_action(m_editor->undo_action());
  100. toolbar->add_action(m_editor->redo_action());
  101. m_editor->set_focus(true);
  102. }
  103. TextEditorWidget::~TextEditorWidget()
  104. {
  105. }
  106. void TextEditorWidget::open_sesame(const String& path)
  107. {
  108. dbgprintf("Our path to file in open_sesame: %s\n", path.characters());
  109. CFile file(path);
  110. if (!file.open(CIODevice::ReadOnly)) {
  111. GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, window());
  112. }
  113. window()->set_title(String::format("Text Editor: %s", path.characters()));
  114. m_editor->set_text(String::copy(file.read_all()));
  115. m_path = path;
  116. }