main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "CppLexer.h"
  2. #include "Editor.h"
  3. #include "EditorWrapper.h"
  4. #include "FindInFilesWidget.h"
  5. #include "Project.h"
  6. #include "TerminalWrapper.h"
  7. #include <LibCore/CFile.h>
  8. #include <LibGUI/GAboutDialog.h>
  9. #include <LibGUI/GAction.h>
  10. #include <LibGUI/GApplication.h>
  11. #include <LibGUI/GBoxLayout.h>
  12. #include <LibGUI/GButton.h>
  13. #include <LibGUI/GFilePicker.h>
  14. #include <LibGUI/GInputBox.h>
  15. #include <LibGUI/GLabel.h>
  16. #include <LibGUI/GListView.h>
  17. #include <LibGUI/GMenu.h>
  18. #include <LibGUI/GMenuBar.h>
  19. #include <LibGUI/GMessageBox.h>
  20. #include <LibGUI/GSplitter.h>
  21. #include <LibGUI/GTabWidget.h>
  22. #include <LibGUI/GTextBox.h>
  23. #include <LibGUI/GTextEditor.h>
  24. #include <LibGUI/GToolBar.h>
  25. #include <LibGUI/GWidget.h>
  26. #include <LibGUI/GWindow.h>
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. NonnullRefPtrVector<EditorWrapper> g_all_editor_wrappers;
  30. RefPtr<EditorWrapper> g_current_editor_wrapper;
  31. String g_currently_open_file;
  32. OwnPtr<Project> g_project;
  33. RefPtr<GWindow> g_window;
  34. RefPtr<GListView> g_project_list_view;
  35. void add_new_editor(GWidget& parent)
  36. {
  37. auto wrapper = EditorWrapper::construct(&parent);
  38. g_current_editor_wrapper = wrapper;
  39. g_all_editor_wrappers.append(wrapper);
  40. }
  41. EditorWrapper& current_editor_wrapper()
  42. {
  43. ASSERT(g_current_editor_wrapper);
  44. return *g_current_editor_wrapper;
  45. }
  46. Editor& current_editor()
  47. {
  48. return current_editor_wrapper().editor();
  49. }
  50. static void build(TerminalWrapper&);
  51. static void run(TerminalWrapper&);
  52. void open_file(const String&);
  53. int main(int argc, char** argv)
  54. {
  55. GApplication app(argc, argv);
  56. g_window = GWindow::construct();
  57. g_window->set_rect(100, 100, 800, 600);
  58. g_window->set_title("HackStudio");
  59. auto widget = GWidget::construct();
  60. g_window->set_main_widget(widget);
  61. widget->set_fill_with_background_color(true);
  62. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  63. widget->layout()->set_spacing(0);
  64. if (chdir("/home/anon/little") < 0) {
  65. perror("chdir");
  66. return 1;
  67. }
  68. g_project = Project::load_from_file("little.files");
  69. ASSERT(g_project);
  70. auto toolbar = GToolBar::construct(widget);
  71. auto outer_splitter = GSplitter::construct(Orientation::Horizontal, widget);
  72. g_project_list_view = GListView::construct(outer_splitter);
  73. g_project_list_view->set_model(g_project->model());
  74. g_project_list_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  75. g_project_list_view->set_preferred_size(200, 0);
  76. auto inner_splitter = GSplitter::construct(Orientation::Vertical, outer_splitter);
  77. inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  78. add_new_editor(inner_splitter);
  79. add_new_editor(inner_splitter);
  80. auto new_action = GAction::create("Add new file to project...", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GAction&) {
  81. auto input_box = GInputBox::construct("Enter name of new file:", "Add new file to project", g_window);
  82. if (input_box->exec() == GInputBox::ExecCancel)
  83. return;
  84. auto filename = input_box->text_value();
  85. auto file = CFile::construct(filename);
  86. if (!file->open((CIODevice::OpenMode)(CIODevice::WriteOnly | CIODevice::MustBeNew))) {
  87. GMessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  88. return;
  89. }
  90. if (!g_project->add_file(filename)) {
  91. GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  92. // FIXME: Should we unlink the file here maybe?
  93. return;
  94. }
  95. open_file(filename);
  96. });
  97. auto add_existing_file_action = GAction::create("Add existing file to project...", GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  98. auto result = GFilePicker::get_open_filepath("Add existing file to project");
  99. if (!result.has_value())
  100. return;
  101. auto& filename = result.value();
  102. if (!g_project->add_file(filename)) {
  103. GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  104. return;
  105. }
  106. open_file(filename);
  107. });
  108. auto switch_to_next_editor = GAction::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  109. if (g_all_editor_wrappers.size() <= 1)
  110. return;
  111. // FIXME: This will only work correctly when there are 2 editors. Make it work for any editor count.
  112. for (auto& wrapper : g_all_editor_wrappers) {
  113. if (&wrapper == &current_editor_wrapper())
  114. continue;
  115. wrapper.editor().set_focus(true);
  116. return;
  117. }
  118. });
  119. auto save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  120. if (g_currently_open_file.is_empty())
  121. return;
  122. current_editor().write_to_file(g_currently_open_file);
  123. });
  124. toolbar->add_action(new_action);
  125. toolbar->add_action(add_existing_file_action);
  126. toolbar->add_action(save_action);
  127. toolbar->add_separator();
  128. toolbar->add_action(GCommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  129. toolbar->add_action(GCommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  130. toolbar->add_action(GCommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  131. toolbar->add_separator();
  132. toolbar->add_action(GCommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  133. toolbar->add_action(GCommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  134. toolbar->add_separator();
  135. g_project_list_view->on_activation = [&](auto& index) {
  136. auto filename = g_project_list_view->model()->data(index).to_string();
  137. open_file(filename);
  138. };
  139. auto tab_widget = GTabWidget::construct(inner_splitter);
  140. tab_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  141. tab_widget->set_preferred_size(0, 24);
  142. auto reveal_action_tab = [&](auto& widget) {
  143. dbg() << "tab_widget preferred height: " << tab_widget->preferred_size().height();
  144. if (tab_widget->preferred_size().height() < 200)
  145. tab_widget->set_preferred_size(0, 200);
  146. tab_widget->set_active_widget(widget);
  147. };
  148. auto hide_action_tabs = [&] {
  149. tab_widget->set_preferred_size(0, 24);
  150. };
  151. auto hide_action_tabs_action = GAction::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  152. hide_action_tabs();
  153. });
  154. auto find_in_files_widget = FindInFilesWidget::construct(nullptr);
  155. tab_widget->add_widget("Find in files", find_in_files_widget);
  156. auto terminal_wrapper = TerminalWrapper::construct(nullptr);
  157. tab_widget->add_widget("Console", terminal_wrapper);
  158. auto menubar = make<GMenuBar>();
  159. auto app_menu = make<GMenu>("HackStudio");
  160. app_menu->add_action(save_action);
  161. app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
  162. app.quit();
  163. }));
  164. menubar->add_menu(move(app_menu));
  165. auto project_menu = make<GMenu>("Project");
  166. project_menu->add_action(new_action);
  167. project_menu->add_action(add_existing_file_action);
  168. menubar->add_menu(move(project_menu));
  169. auto edit_menu = make<GMenu>("Edit");
  170. edit_menu->add_action(GAction::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  171. reveal_action_tab(find_in_files_widget);
  172. find_in_files_widget->focus_textbox_and_select_all();
  173. }));
  174. menubar->add_menu(move(edit_menu));
  175. auto build_action = GAction::create("Build", { Mod_Ctrl, Key_B }, GraphicsBitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  176. reveal_action_tab(terminal_wrapper);
  177. build(terminal_wrapper);
  178. });
  179. toolbar->add_action(build_action);
  180. auto run_action = GAction::create("Run", { Mod_Ctrl, Key_R }, GraphicsBitmap::load_from_file("/res/icons/16x16/run.png"), [&](auto&) {
  181. reveal_action_tab(terminal_wrapper);
  182. run(terminal_wrapper);
  183. });
  184. toolbar->add_action(run_action);
  185. auto build_menu = make<GMenu>("Build");
  186. build_menu->add_action(build_action);
  187. build_menu->add_action(run_action);
  188. menubar->add_menu(move(build_menu));
  189. auto view_menu = make<GMenu>("View");
  190. view_menu->add_action(hide_action_tabs_action);
  191. menubar->add_menu(move(view_menu));
  192. auto small_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  193. auto help_menu = make<GMenu>("Help");
  194. help_menu->add_action(GAction::create("About", [&](auto&) {
  195. GAboutDialog::show("HackStudio", small_icon, g_window);
  196. }));
  197. menubar->add_menu(move(help_menu));
  198. app.set_menubar(move(menubar));
  199. g_window->set_icon(small_icon);
  200. g_window->show();
  201. open_file("main.cpp");
  202. return app.exec();
  203. }
  204. void build(TerminalWrapper& wrapper)
  205. {
  206. wrapper.run_command("make");
  207. }
  208. void run(TerminalWrapper& wrapper)
  209. {
  210. wrapper.run_command("make run");
  211. }
  212. struct TextStyle {
  213. Color color;
  214. const Font* font { nullptr };
  215. };
  216. static TextStyle style_for_token_type(CppToken::Type type)
  217. {
  218. switch (type) {
  219. case CppToken::Type::Keyword:
  220. return { Color::Black, &Font::default_bold_fixed_width_font() };
  221. case CppToken::Type::KnownType:
  222. return { Color::from_rgb(0x929200), &Font::default_bold_fixed_width_font() };
  223. case CppToken::Type::Identifier:
  224. return { Color::from_rgb(0x000092) };
  225. case CppToken::Type::DoubleQuotedString:
  226. case CppToken::Type::SingleQuotedString:
  227. case CppToken::Type::Number:
  228. return { Color::from_rgb(0x920000) };
  229. case CppToken::Type::PreprocessorStatement:
  230. return { Color::from_rgb(0x009292) };
  231. case CppToken::Type::Comment:
  232. return { Color::from_rgb(0x009200) };
  233. default:
  234. return { Color::Black };
  235. }
  236. }
  237. static void rehighlight()
  238. {
  239. auto text = current_editor().text();
  240. CppLexer lexer(text);
  241. auto tokens = lexer.lex();
  242. Vector<GTextDocumentSpan> spans;
  243. for (auto& token : tokens) {
  244. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  245. dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
  246. #endif
  247. GTextDocumentSpan span;
  248. span.range.set_start({ token.m_start.line, token.m_start.column });
  249. span.range.set_end({ token.m_end.line, token.m_end.column });
  250. auto style = style_for_token_type(token.m_type);
  251. span.color = style.color;
  252. span.font = style.font;
  253. spans.append(span);
  254. }
  255. current_editor().document().set_spans(spans);
  256. current_editor().update();
  257. }
  258. void open_file(const String& filename)
  259. {
  260. auto file = g_project->get_file(filename);
  261. current_editor().set_document(const_cast<GTextDocument&>(file->document()));
  262. if (filename.ends_with(".cpp") || filename.ends_with(".h")) {
  263. current_editor().on_change = [] { rehighlight(); };
  264. rehighlight();
  265. } else {
  266. current_editor().on_change = nullptr;
  267. }
  268. g_currently_open_file = filename;
  269. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  270. g_project_list_view->update();
  271. current_editor_wrapper().filename_label().set_text(filename);
  272. current_editor().set_focus(true);
  273. }