main.cpp 11 KB

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