TextEditorWidget.cpp 6.8 KB

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