main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include "CppLexer.h"
  2. #include "Editor.h"
  3. #include "EditorWrapper.h"
  4. #include "FindInFilesWidget.h"
  5. #include "FormEditorWidget.h"
  6. #include "Locator.h"
  7. #include "Project.h"
  8. #include "TerminalWrapper.h"
  9. #include <LibCore/CFile.h>
  10. #include <LibGUI/GAboutDialog.h>
  11. #include <LibGUI/GAction.h>
  12. #include <LibGUI/GApplication.h>
  13. #include <LibGUI/GBoxLayout.h>
  14. #include <LibGUI/GButton.h>
  15. #include <LibGUI/GFilePicker.h>
  16. #include <LibGUI/GInputBox.h>
  17. #include <LibGUI/GLabel.h>
  18. #include <LibGUI/GListView.h>
  19. #include <LibGUI/GMenu.h>
  20. #include <LibGUI/GMenuBar.h>
  21. #include <LibGUI/GMessageBox.h>
  22. #include <LibGUI/GSplitter.h>
  23. #include <LibGUI/GStackWidget.h>
  24. #include <LibGUI/GTabWidget.h>
  25. #include <LibGUI/GTextBox.h>
  26. #include <LibGUI/GTextEditor.h>
  27. #include <LibGUI/GToolBar.h>
  28. #include <LibGUI/GWidget.h>
  29. #include <LibGUI/GWindow.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. NonnullRefPtrVector<EditorWrapper> g_all_editor_wrappers;
  33. RefPtr<EditorWrapper> g_current_editor_wrapper;
  34. String g_currently_open_file;
  35. OwnPtr<Project> g_project;
  36. RefPtr<GWindow> g_window;
  37. RefPtr<GListView> g_project_list_view;
  38. RefPtr<GStackWidget> g_right_hand_stack;
  39. RefPtr<GSplitter> g_text_inner_splitter;
  40. RefPtr<GWidget> g_form_inner_container;
  41. RefPtr<FormEditorWidget> g_form_editor_widget;
  42. static RefPtr<GTabWidget> s_action_tab_widget;
  43. void add_new_editor(GWidget& parent)
  44. {
  45. auto wrapper = EditorWrapper::construct(nullptr);
  46. if (s_action_tab_widget) {
  47. parent.insert_child_before(wrapper, *s_action_tab_widget);
  48. } else {
  49. parent.add_child(wrapper);
  50. }
  51. g_current_editor_wrapper = wrapper;
  52. g_all_editor_wrappers.append(wrapper);
  53. wrapper->editor().set_focus(true);
  54. }
  55. enum class EditMode {
  56. Text,
  57. Form,
  58. };
  59. void set_edit_mode(EditMode mode)
  60. {
  61. if (mode == EditMode::Text) {
  62. g_right_hand_stack->set_active_widget(g_text_inner_splitter);
  63. } else if (mode == EditMode::Form) {
  64. g_right_hand_stack->set_active_widget(g_form_inner_container);
  65. }
  66. }
  67. EditorWrapper& current_editor_wrapper()
  68. {
  69. ASSERT(g_current_editor_wrapper);
  70. return *g_current_editor_wrapper;
  71. }
  72. Editor& current_editor()
  73. {
  74. return current_editor_wrapper().editor();
  75. }
  76. static void build(TerminalWrapper&);
  77. static void run(TerminalWrapper&);
  78. void open_file(const String&);
  79. int main(int argc, char** argv)
  80. {
  81. GApplication app(argc, argv);
  82. Function<void()> update_actions;
  83. g_window = GWindow::construct();
  84. g_window->set_rect(100, 100, 800, 600);
  85. g_window->set_title("HackStudio");
  86. auto widget = GWidget::construct();
  87. g_window->set_main_widget(widget);
  88. widget->set_fill_with_background_color(true);
  89. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  90. widget->layout()->set_spacing(0);
  91. if (chdir("/home/anon/little") < 0) {
  92. perror("chdir");
  93. return 1;
  94. }
  95. g_project = Project::load_from_file("little.files");
  96. ASSERT(g_project);
  97. auto toolbar = GToolBar::construct(widget);
  98. auto outer_splitter = GSplitter::construct(Orientation::Horizontal, widget);
  99. g_project_list_view = GListView::construct(outer_splitter);
  100. g_project_list_view->set_model(g_project->model());
  101. g_project_list_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  102. g_project_list_view->set_preferred_size(140, 0);
  103. g_right_hand_stack = GStackWidget::construct(outer_splitter);
  104. g_form_inner_container = GWidget::construct(g_right_hand_stack);
  105. g_form_inner_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  106. auto form_widgets_toolbar = GToolBar::construct(Orientation::Vertical, 26, g_form_inner_container);
  107. form_widgets_toolbar->set_preferred_size(38, 0);
  108. form_widgets_toolbar->add_action(GAction::create("GLabel", GraphicsBitmap::load_from_file("/res/icons/vbwidgets/label.png"), [&](auto&) {}));
  109. form_widgets_toolbar->add_action(GAction::create("GButton", GraphicsBitmap::load_from_file("/res/icons/vbwidgets/button.png"), [&](auto&) {}));
  110. form_widgets_toolbar->add_action(GAction::create("GSpinBox", GraphicsBitmap::load_from_file("/res/icons/vbwidgets/spinbox.png"), [&](auto&) {}));
  111. g_form_editor_widget = FormEditorWidget::construct(g_form_inner_container);
  112. g_text_inner_splitter = GSplitter::construct(Orientation::Vertical, g_right_hand_stack);
  113. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  114. add_new_editor(*g_text_inner_splitter);
  115. 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&) {
  116. auto input_box = GInputBox::construct("Enter name of new file:", "Add new file to project", g_window);
  117. if (input_box->exec() == GInputBox::ExecCancel)
  118. return;
  119. auto filename = input_box->text_value();
  120. auto file = CFile::construct(filename);
  121. if (!file->open((CIODevice::OpenMode)(CIODevice::WriteOnly | CIODevice::MustBeNew))) {
  122. GMessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  123. return;
  124. }
  125. if (!g_project->add_file(filename)) {
  126. GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  127. // FIXME: Should we unlink the file here maybe?
  128. return;
  129. }
  130. open_file(filename);
  131. });
  132. auto add_existing_file_action = GAction::create("Add existing file to project...", GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  133. auto result = GFilePicker::get_open_filepath("Add existing file to project");
  134. if (!result.has_value())
  135. return;
  136. auto& filename = result.value();
  137. if (!g_project->add_file(filename)) {
  138. GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  139. return;
  140. }
  141. open_file(filename);
  142. });
  143. auto switch_to_next_editor = GAction::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  144. if (g_all_editor_wrappers.size() <= 1)
  145. return;
  146. Vector<EditorWrapper*> wrappers;
  147. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  148. wrappers.append(&child);
  149. return IterationDecision::Continue;
  150. });
  151. for (int i = 0; i < wrappers.size(); ++i) {
  152. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  153. if (i == wrappers.size() - 1)
  154. wrappers[0]->editor().set_focus(true);
  155. else
  156. wrappers[i + 1]->editor().set_focus(true);
  157. }
  158. }
  159. });
  160. auto switch_to_previous_editor = GAction::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  161. if (g_all_editor_wrappers.size() <= 1)
  162. return;
  163. Vector<EditorWrapper*> wrappers;
  164. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  165. wrappers.append(&child);
  166. return IterationDecision::Continue;
  167. });
  168. for (int i = wrappers.size() - 1; i >= 0; --i) {
  169. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  170. if (i == 0)
  171. wrappers.last()->editor().set_focus(true);
  172. else
  173. wrappers[i - 1]->editor().set_focus(true);
  174. }
  175. }
  176. });
  177. auto remove_current_editor_action = GAction::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  178. if (g_all_editor_wrappers.size() <= 1)
  179. return;
  180. auto wrapper = g_current_editor_wrapper;
  181. switch_to_next_editor->activate();
  182. g_text_inner_splitter->remove_child(*wrapper);
  183. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  184. update_actions();
  185. });
  186. auto save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  187. if (g_currently_open_file.is_empty())
  188. return;
  189. current_editor().write_to_file(g_currently_open_file);
  190. });
  191. toolbar->add_action(new_action);
  192. toolbar->add_action(add_existing_file_action);
  193. toolbar->add_action(save_action);
  194. toolbar->add_separator();
  195. toolbar->add_action(GCommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  196. toolbar->add_action(GCommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  197. toolbar->add_action(GCommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  198. toolbar->add_separator();
  199. toolbar->add_action(GCommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  200. toolbar->add_action(GCommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  201. toolbar->add_separator();
  202. g_project_list_view->on_activation = [&](auto& index) {
  203. auto filename = g_project_list_view->model()->data(index).to_string();
  204. open_file(filename);
  205. };
  206. s_action_tab_widget = GTabWidget::construct(g_text_inner_splitter);
  207. s_action_tab_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  208. s_action_tab_widget->set_preferred_size(0, 24);
  209. auto reveal_action_tab = [&](auto& widget) {
  210. if (s_action_tab_widget->preferred_size().height() < 200)
  211. s_action_tab_widget->set_preferred_size(0, 200);
  212. s_action_tab_widget->set_active_widget(widget);
  213. };
  214. auto hide_action_tabs = [&] {
  215. s_action_tab_widget->set_preferred_size(0, 24);
  216. };
  217. auto hide_action_tabs_action = GAction::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  218. hide_action_tabs();
  219. });
  220. auto add_editor_action = GAction::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E }, [&](auto&) {
  221. add_new_editor(*g_text_inner_splitter);
  222. update_actions();
  223. });
  224. auto find_in_files_widget = FindInFilesWidget::construct(nullptr);
  225. s_action_tab_widget->add_widget("Find in files", find_in_files_widget);
  226. auto terminal_wrapper = TerminalWrapper::construct(nullptr);
  227. s_action_tab_widget->add_widget("Console", terminal_wrapper);
  228. auto locator = Locator::construct(widget);
  229. auto open_locator_action = GAction::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  230. locator->open();
  231. });
  232. auto menubar = make<GMenuBar>();
  233. auto app_menu = make<GMenu>("HackStudio");
  234. app_menu->add_action(save_action);
  235. app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
  236. app.quit();
  237. }));
  238. menubar->add_menu(move(app_menu));
  239. auto project_menu = make<GMenu>("Project");
  240. project_menu->add_action(new_action);
  241. project_menu->add_action(add_existing_file_action);
  242. menubar->add_menu(move(project_menu));
  243. auto edit_menu = make<GMenu>("Edit");
  244. edit_menu->add_action(GAction::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  245. reveal_action_tab(find_in_files_widget);
  246. find_in_files_widget->focus_textbox_and_select_all();
  247. }));
  248. menubar->add_menu(move(edit_menu));
  249. auto build_action = GAction::create("Build", { Mod_Ctrl, Key_B }, GraphicsBitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  250. reveal_action_tab(terminal_wrapper);
  251. build(terminal_wrapper);
  252. });
  253. toolbar->add_action(build_action);
  254. auto run_action = GAction::create("Run", { Mod_Ctrl, Key_R }, GraphicsBitmap::load_from_file("/res/icons/16x16/run.png"), [&](auto&) {
  255. reveal_action_tab(terminal_wrapper);
  256. run(terminal_wrapper);
  257. });
  258. toolbar->add_action(run_action);
  259. auto build_menu = make<GMenu>("Build");
  260. build_menu->add_action(build_action);
  261. build_menu->add_action(run_action);
  262. menubar->add_menu(move(build_menu));
  263. auto view_menu = make<GMenu>("View");
  264. view_menu->add_action(hide_action_tabs_action);
  265. view_menu->add_action(open_locator_action);
  266. view_menu->add_separator();
  267. view_menu->add_action(add_editor_action);
  268. view_menu->add_action(remove_current_editor_action);
  269. menubar->add_menu(move(view_menu));
  270. auto small_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  271. auto help_menu = make<GMenu>("Help");
  272. help_menu->add_action(GAction::create("About", [&](auto&) {
  273. GAboutDialog::show("HackStudio", small_icon, g_window);
  274. }));
  275. menubar->add_menu(move(help_menu));
  276. app.set_menubar(move(menubar));
  277. g_window->set_icon(small_icon);
  278. g_window->show();
  279. update_actions = [&]() {
  280. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  281. };
  282. open_file("test.frm");
  283. update_actions();
  284. return app.exec();
  285. }
  286. void build(TerminalWrapper& wrapper)
  287. {
  288. wrapper.run_command("make");
  289. }
  290. void run(TerminalWrapper& wrapper)
  291. {
  292. wrapper.run_command("make run");
  293. }
  294. struct TextStyle {
  295. Color color;
  296. const Font* font { nullptr };
  297. };
  298. static TextStyle style_for_token_type(CppToken::Type type)
  299. {
  300. switch (type) {
  301. case CppToken::Type::Keyword:
  302. return { Color::Black, &Font::default_bold_fixed_width_font() };
  303. case CppToken::Type::KnownType:
  304. return { Color::from_rgb(0x929200), &Font::default_bold_fixed_width_font() };
  305. case CppToken::Type::Identifier:
  306. return { Color::from_rgb(0x000092) };
  307. case CppToken::Type::DoubleQuotedString:
  308. case CppToken::Type::SingleQuotedString:
  309. case CppToken::Type::Number:
  310. return { Color::from_rgb(0x920000) };
  311. case CppToken::Type::PreprocessorStatement:
  312. return { Color::from_rgb(0x009292) };
  313. case CppToken::Type::Comment:
  314. return { Color::from_rgb(0x009200) };
  315. default:
  316. return { Color::Black };
  317. }
  318. }
  319. static void rehighlight()
  320. {
  321. auto text = current_editor().text();
  322. CppLexer lexer(text);
  323. auto tokens = lexer.lex();
  324. Vector<GTextDocumentSpan> spans;
  325. for (auto& token : tokens) {
  326. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  327. dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
  328. #endif
  329. GTextDocumentSpan span;
  330. span.range.set_start({ token.m_start.line, token.m_start.column });
  331. span.range.set_end({ token.m_end.line, token.m_end.column });
  332. auto style = style_for_token_type(token.m_type);
  333. span.color = style.color;
  334. span.font = style.font;
  335. spans.append(span);
  336. }
  337. current_editor().document().set_spans(spans);
  338. current_editor().update();
  339. }
  340. void open_file(const String& filename)
  341. {
  342. auto file = g_project->get_file(filename);
  343. current_editor().set_document(const_cast<GTextDocument&>(file->document()));
  344. if (filename.ends_with(".cpp") || filename.ends_with(".h")) {
  345. current_editor().on_change = [] { rehighlight(); };
  346. rehighlight();
  347. } else {
  348. current_editor().on_change = nullptr;
  349. }
  350. if (filename.ends_with(".frm")) {
  351. set_edit_mode(EditMode::Form);
  352. } else {
  353. set_edit_mode(EditMode::Text);
  354. }
  355. g_currently_open_file = filename;
  356. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  357. g_project_list_view->update();
  358. current_editor_wrapper().filename_label().set_text(filename);
  359. current_editor().set_focus(true);
  360. }