main.cpp 5.0 KB

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