TextEditorWidget.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/GButton.h>
  8. #include <LibGUI/GFilePicker.h>
  9. #include <LibGUI/GFontDatabase.h>
  10. #include <LibGUI/GMenuBar.h>
  11. #include <LibGUI/GMessageBox.h>
  12. #include <LibGUI/GStatusBar.h>
  13. #include <LibGUI/GTextBox.h>
  14. #include <LibGUI/GTextEditor.h>
  15. #include <LibGUI/GToolBar.h>
  16. TextEditorWidget::TextEditorWidget()
  17. {
  18. set_layout(make<GBoxLayout>(Orientation::Vertical));
  19. layout()->set_spacing(0);
  20. auto* toolbar = new GToolBar(this);
  21. m_editor = new GTextEditor(GTextEditor::MultiLine, this);
  22. m_editor->set_ruler_visible(true);
  23. m_editor->set_automatic_indentation_enabled(true);
  24. m_find_widget = new GWidget(this);
  25. m_find_widget->set_fill_with_background_color(true);
  26. m_find_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  27. m_find_widget->set_preferred_size(0, 22);
  28. m_find_widget->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  29. m_find_widget->layout()->set_margins({ 2, 2, 2, 2 });
  30. m_find_widget->set_visible(false);
  31. m_find_textbox = new GTextBox(m_find_widget);
  32. m_find_button = new GButton("Find", m_find_widget);
  33. m_find_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  34. m_find_button->set_preferred_size(100, 0);
  35. m_find_button->on_click = [this](auto&) {
  36. auto needle = m_find_textbox->text();
  37. auto found_range = m_editor->find(needle, m_editor->normalized_selection().end());
  38. dbg() << "find(\"" << needle << "\") returned " << found_range;
  39. if (found_range.is_valid()) {
  40. m_editor->set_selection(found_range);
  41. } else {
  42. GMessageBox::show(
  43. String::format("Not found: \"%s\"", needle.characters()),
  44. "Not found",
  45. GMessageBox::Type::Information,
  46. GMessageBox::InputType::OK, window());
  47. }
  48. };
  49. m_find_textbox->on_return_pressed = [this] {
  50. m_find_button->click();
  51. };
  52. m_find_textbox->on_escape_pressed = [this] {
  53. m_find_widget->set_visible(false);
  54. m_editor->set_focus(true);
  55. };
  56. m_find_action = GAction::create("Find...", { Mod_Ctrl, Key_F }, [this](auto&) {
  57. m_find_widget->set_visible(true);
  58. m_find_textbox->set_focus(true);
  59. });
  60. auto* statusbar = new GStatusBar(this);
  61. m_editor->on_cursor_change = [statusbar, this] {
  62. StringBuilder builder;
  63. builder.appendf("Line: %d, Column: %d", m_editor->cursor().line() + 1, m_editor->cursor().column());
  64. statusbar->set_text(builder.to_string());
  65. };
  66. m_new_action = GAction::create("New", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [](const GAction&) {
  67. dbgprintf("FIXME: Implement File/New\n");
  68. });
  69. m_open_action = GAction::create("Open...", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [this](const GAction&) {
  70. Optional<String> open_path = GFilePicker::get_open_filepath();
  71. if (!open_path.has_value())
  72. return;
  73. open_sesame(open_path.value());
  74. });
  75. m_save_as_action = GAction::create("Save as...", { Mod_None, Key_F12 }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
  76. Optional<String> save_path = GFilePicker::get_save_filepath(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension);
  77. if (!save_path.has_value())
  78. return;
  79. if (!m_editor->write_to_file(save_path.value())) {
  80. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  81. return;
  82. }
  83. set_path(FileSystemPath(save_path.value()));
  84. dbg() << "Wrote document to " << save_path.value();
  85. });
  86. m_save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GAction&) {
  87. if (!m_path.is_empty()) {
  88. if (!m_editor->write_to_file(m_path))
  89. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  90. return;
  91. }
  92. m_save_as_action->activate();
  93. });
  94. auto menubar = make<GMenuBar>();
  95. auto app_menu = make<GMenu>("Text Editor");
  96. app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) {
  97. GApplication::the().quit(0);
  98. return;
  99. }));
  100. menubar->add_menu(move(app_menu));
  101. auto file_menu = make<GMenu>("File");
  102. file_menu->add_action(*m_new_action);
  103. file_menu->add_action(*m_open_action);
  104. file_menu->add_action(*m_save_action);
  105. file_menu->add_action(*m_save_as_action);
  106. menubar->add_menu(move(file_menu));
  107. auto edit_menu = make<GMenu>("Edit");
  108. edit_menu->add_action(m_editor->undo_action());
  109. edit_menu->add_action(m_editor->redo_action());
  110. edit_menu->add_separator();
  111. edit_menu->add_action(m_editor->cut_action());
  112. edit_menu->add_action(m_editor->copy_action());
  113. edit_menu->add_action(m_editor->paste_action());
  114. edit_menu->add_action(m_editor->delete_action());
  115. edit_menu->add_separator();
  116. edit_menu->add_action(*m_find_action);
  117. menubar->add_menu(move(edit_menu));
  118. auto font_menu = make<GMenu>("Font");
  119. GFontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
  120. font_menu->add_action(GAction::create(font_name, [this](const GAction& action) {
  121. m_editor->set_font(GFontDatabase::the().get_by_name(action.text()));
  122. m_editor->update();
  123. }));
  124. });
  125. menubar->add_menu(move(font_menu));
  126. auto help_menu = make<GMenu>("Help");
  127. help_menu->add_action(GAction::create("About", [](const GAction&) {
  128. dbgprintf("FIXME: Implement Help/About\n");
  129. }));
  130. menubar->add_menu(move(help_menu));
  131. GApplication::the().set_menubar(move(menubar));
  132. toolbar->add_action(*m_new_action);
  133. toolbar->add_action(*m_open_action);
  134. toolbar->add_action(*m_save_action);
  135. toolbar->add_separator();
  136. toolbar->add_action(m_editor->cut_action());
  137. toolbar->add_action(m_editor->copy_action());
  138. toolbar->add_action(m_editor->paste_action());
  139. toolbar->add_action(m_editor->delete_action());
  140. toolbar->add_separator();
  141. toolbar->add_action(m_editor->undo_action());
  142. toolbar->add_action(m_editor->redo_action());
  143. m_editor->set_focus(true);
  144. }
  145. TextEditorWidget::~TextEditorWidget()
  146. {
  147. }
  148. void TextEditorWidget::set_path(const FileSystemPath& file)
  149. {
  150. m_path = file.string();
  151. m_name = file.title();
  152. m_extension = file.extension();
  153. StringBuilder builder;
  154. builder.append("Text Editor: ");
  155. builder.append(file.string());
  156. window()->set_title(builder.to_string());
  157. }
  158. void TextEditorWidget::open_sesame(const String& path)
  159. {
  160. dbgprintf("Our path to file in open_sesame: %s\n", path.characters());
  161. CFile file(path);
  162. if (!file.open(CIODevice::ReadOnly)) {
  163. GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  164. }
  165. m_editor->set_text(file.read_all());
  166. set_path(FileSystemPath(path));
  167. }