TextEditorWidget.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. m_new_action = GAction::create("New", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [](const GAction&) {
  29. dbgprintf("FIXME: Implement File/New\n");
  30. });
  31. m_open_action = GAction::create("Open...", { 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. open_sesame(open_name.value());
  36. });
  37. m_save_as_action = GAction::create("Save as...", { Mod_None, Key_F12 }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
  38. Optional<String> save_name = GFilePicker::get_save_filepath();
  39. if (!save_name.has_value())
  40. return;
  41. if (!m_editor->write_to_file(save_name.value())) {
  42. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  43. return;
  44. }
  45. set_path(save_name.value());
  46. dbg() << "Wrote document to " << save_name.value();
  47. });
  48. m_save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GAction&) {
  49. if (!m_path.is_empty()) {
  50. if (!m_editor->write_to_file(m_path))
  51. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  52. return;
  53. }
  54. m_save_as_action->activate();
  55. });
  56. auto menubar = make<GMenuBar>();
  57. auto app_menu = make<GMenu>("Text Editor");
  58. app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) {
  59. GApplication::the().quit(0);
  60. return;
  61. }));
  62. menubar->add_menu(move(app_menu));
  63. auto file_menu = make<GMenu>("File");
  64. file_menu->add_action(*m_new_action);
  65. file_menu->add_action(*m_open_action);
  66. file_menu->add_action(*m_save_action);
  67. file_menu->add_action(*m_save_as_action);
  68. menubar->add_menu(move(file_menu));
  69. auto edit_menu = make<GMenu>("Edit");
  70. edit_menu->add_action(m_editor->undo_action());
  71. edit_menu->add_action(m_editor->redo_action());
  72. edit_menu->add_separator();
  73. edit_menu->add_action(m_editor->cut_action());
  74. edit_menu->add_action(m_editor->copy_action());
  75. edit_menu->add_action(m_editor->paste_action());
  76. edit_menu->add_action(m_editor->delete_action());
  77. menubar->add_menu(move(edit_menu));
  78. auto font_menu = make<GMenu>("Font");
  79. GFontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
  80. font_menu->add_action(GAction::create(font_name, [this](const GAction& action) {
  81. m_editor->set_font(GFontDatabase::the().get_by_name(action.text()));
  82. m_editor->update();
  83. }));
  84. });
  85. menubar->add_menu(move(font_menu));
  86. auto help_menu = make<GMenu>("Help");
  87. help_menu->add_action(GAction::create("About", [](const GAction&) {
  88. dbgprintf("FIXME: Implement Help/About\n");
  89. }));
  90. menubar->add_menu(move(help_menu));
  91. GApplication::the().set_menubar(move(menubar));
  92. toolbar->add_action(*m_new_action);
  93. toolbar->add_action(*m_open_action);
  94. toolbar->add_action(*m_save_action);
  95. toolbar->add_separator();
  96. toolbar->add_action(m_editor->cut_action());
  97. toolbar->add_action(m_editor->copy_action());
  98. toolbar->add_action(m_editor->paste_action());
  99. toolbar->add_action(m_editor->delete_action());
  100. toolbar->add_separator();
  101. toolbar->add_action(m_editor->undo_action());
  102. toolbar->add_action(m_editor->redo_action());
  103. m_editor->set_focus(true);
  104. }
  105. TextEditorWidget::~TextEditorWidget()
  106. {
  107. }
  108. void TextEditorWidget::set_path(const StringView& path)
  109. {
  110. m_path = path;
  111. StringBuilder builder;
  112. builder.append("Text Editor: ");
  113. builder.append(path);
  114. window()->set_title(builder.to_string());
  115. }
  116. void TextEditorWidget::open_sesame(const String& path)
  117. {
  118. dbgprintf("Our path to file in open_sesame: %s\n", path.characters());
  119. CFile file(path);
  120. if (!file.open(CIODevice::ReadOnly)) {
  121. GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  122. }
  123. m_editor->set_text(String::copy(file.read_all()));
  124. set_path(path);
  125. }