main.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "CppLexer.h"
  27. #include "CursorTool.h"
  28. #include "Editor.h"
  29. #include "EditorWrapper.h"
  30. #include "FindInFilesWidget.h"
  31. #include "FormEditorWidget.h"
  32. #include "FormWidget.h"
  33. #include "Locator.h"
  34. #include "Project.h"
  35. #include "TerminalWrapper.h"
  36. #include "WidgetTool.h"
  37. #include "WidgetTreeModel.h"
  38. #include <AK/StringBuilder.h>
  39. #include <LibCore/File.h>
  40. #include <LibGUI/GAboutDialog.h>
  41. #include <LibGUI/GAction.h>
  42. #include <LibGUI/GActionGroup.h>
  43. #include <LibGUI/GApplication.h>
  44. #include <LibGUI/GBoxLayout.h>
  45. #include <LibGUI/GButton.h>
  46. #include <LibGUI/GFilePicker.h>
  47. #include <LibGUI/GInputBox.h>
  48. #include <LibGUI/GLabel.h>
  49. #include <LibGUI/GMenu.h>
  50. #include <LibGUI/GMenuBar.h>
  51. #include <LibGUI/GMessageBox.h>
  52. #include <LibGUI/GSplitter.h>
  53. #include <LibGUI/GStackWidget.h>
  54. #include <LibGUI/GTabWidget.h>
  55. #include <LibGUI/GTableView.h>
  56. #include <LibGUI/GTextBox.h>
  57. #include <LibGUI/GTextEditor.h>
  58. #include <LibGUI/GToolBar.h>
  59. #include <LibGUI/GTreeView.h>
  60. #include <LibGUI/GWidget.h>
  61. #include <LibGUI/GWindow.h>
  62. #include <stdio.h>
  63. #include <sys/wait.h>
  64. #include <unistd.h>
  65. NonnullRefPtrVector<EditorWrapper> g_all_editor_wrappers;
  66. RefPtr<EditorWrapper> g_current_editor_wrapper;
  67. String g_currently_open_file;
  68. OwnPtr<Project> g_project;
  69. RefPtr<GUI::Window> g_window;
  70. RefPtr<GUI::TreeView> g_project_tree_view;
  71. RefPtr<GUI::StackWidget> g_right_hand_stack;
  72. RefPtr<GUI::Splitter> g_text_inner_splitter;
  73. RefPtr<GUI::Widget> g_form_inner_container;
  74. RefPtr<FormEditorWidget> g_form_editor_widget;
  75. static RefPtr<GUI::TabWidget> s_action_tab_widget;
  76. void add_new_editor(GUI::Widget& parent)
  77. {
  78. auto wrapper = EditorWrapper::construct(nullptr);
  79. if (s_action_tab_widget) {
  80. parent.insert_child_before(wrapper, *s_action_tab_widget);
  81. } else {
  82. parent.add_child(wrapper);
  83. }
  84. g_current_editor_wrapper = wrapper;
  85. g_all_editor_wrappers.append(wrapper);
  86. wrapper->editor().set_focus(true);
  87. }
  88. enum class EditMode {
  89. Text,
  90. Form,
  91. };
  92. void set_edit_mode(EditMode mode)
  93. {
  94. if (mode == EditMode::Text) {
  95. g_right_hand_stack->set_active_widget(g_text_inner_splitter);
  96. } else if (mode == EditMode::Form) {
  97. g_right_hand_stack->set_active_widget(g_form_inner_container);
  98. }
  99. }
  100. EditorWrapper& current_editor_wrapper()
  101. {
  102. ASSERT(g_current_editor_wrapper);
  103. return *g_current_editor_wrapper;
  104. }
  105. Editor& current_editor()
  106. {
  107. return current_editor_wrapper().editor();
  108. }
  109. static void build(TerminalWrapper&);
  110. static void run(TerminalWrapper&);
  111. void open_file(const String&);
  112. bool make_is_available();
  113. int main(int argc, char** argv)
  114. {
  115. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec unix fattr", nullptr) < 0) {
  116. perror("pledge");
  117. return 1;
  118. }
  119. GUI::Application app(argc, argv);
  120. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec fattr", nullptr) < 0) {
  121. perror("pledge");
  122. return 1;
  123. }
  124. Function<void()> update_actions;
  125. g_window = GUI::Window::construct();
  126. g_window->set_rect(90, 90, 840, 600);
  127. g_window->set_title("HackStudio");
  128. auto widget = GUI::Widget::construct();
  129. g_window->set_main_widget(widget);
  130. widget->set_fill_with_background_color(true);
  131. widget->set_layout(make<GUI::VerticalBoxLayout>());
  132. widget->layout()->set_spacing(0);
  133. StringBuilder path;
  134. path.append(getenv("PATH"));
  135. if (path.length())
  136. path.append(":");
  137. path.append("/bin:/usr/bin:/usr/local/bin");
  138. setenv("PATH", path.to_string().characters(), true);
  139. if (!make_is_available())
  140. GUI::MessageBox::show("The 'make' command is not available. You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository.", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  141. if (chdir("/home/anon/little") < 0) {
  142. perror("chdir");
  143. return 1;
  144. }
  145. g_project = Project::load_from_file("little.files");
  146. ASSERT(g_project);
  147. auto toolbar = GUI::ToolBar::construct(widget);
  148. auto selected_file_names = [&] {
  149. Vector<String> files;
  150. g_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  151. files.append(g_project->model().data(index).as_string());
  152. });
  153. return files;
  154. };
  155. auto new_action = GUI::Action::create("Add new file to project...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
  156. auto input_box = GUI::InputBox::construct("Enter name of new file:", "Add new file to project", g_window);
  157. if (input_box->exec() == GUI::InputBox::ExecCancel)
  158. return;
  159. auto filename = input_box->text_value();
  160. auto file = Core::File::construct(filename);
  161. if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
  162. GUI::MessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  163. return;
  164. }
  165. if (!g_project->add_file(filename)) {
  166. GUI::MessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  167. // FIXME: Should we unlink the file here maybe?
  168. return;
  169. }
  170. open_file(filename);
  171. });
  172. auto add_existing_file_action = GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  173. auto result = GUI::FilePicker::get_open_filepath("Add existing file to project");
  174. if (!result.has_value())
  175. return;
  176. auto& filename = result.value();
  177. if (!g_project->add_file(filename)) {
  178. GUI::MessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  179. return;
  180. }
  181. open_file(filename);
  182. });
  183. auto delete_action = GUI::CommonActions::make_delete_action([&](const GUI::Action& action) {
  184. (void)action;
  185. auto files = selected_file_names();
  186. if (files.is_empty())
  187. return;
  188. String message;
  189. if (files.size() == 1) {
  190. message = String::format("Really remove %s from the project?", FileSystemPath(files[0]).basename().characters());
  191. } else {
  192. message = String::format("Really remove %d files from the project?", files.size());
  193. }
  194. auto result = GUI::MessageBox::show(
  195. message,
  196. "Confirm deletion",
  197. GUI::MessageBox::Type::Warning,
  198. GUI::MessageBox::InputType::OKCancel,
  199. g_window);
  200. if (result == GUI::MessageBox::ExecCancel)
  201. return;
  202. for (auto& file : files) {
  203. if (!g_project->remove_file(file)) {
  204. GUI::MessageBox::show(
  205. String::format("Removing file %s from the project failed.", file.characters()),
  206. "Removal failed",
  207. GUI::MessageBox::Type::Error,
  208. GUI::MessageBox::InputType::OK,
  209. g_window);
  210. break;
  211. }
  212. }
  213. });
  214. delete_action->set_enabled(false);
  215. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  216. project_tree_view_context_menu->add_action(new_action);
  217. project_tree_view_context_menu->add_action(add_existing_file_action);
  218. project_tree_view_context_menu->add_action(delete_action);
  219. auto outer_splitter = GUI::HorizontalSplitter::construct(widget);
  220. g_project_tree_view = GUI::TreeView::construct(outer_splitter);
  221. g_project_tree_view->set_model(g_project->model());
  222. g_project_tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  223. g_project_tree_view->set_preferred_size(140, 0);
  224. g_project_tree_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  225. if (index.is_valid()) {
  226. project_tree_view_context_menu->popup(event.screen_position());
  227. }
  228. };
  229. g_project_tree_view->on_selection_change = [&] {
  230. delete_action->set_enabled(!g_project_tree_view->selection().is_empty());
  231. };
  232. g_right_hand_stack = GUI::StackWidget::construct(outer_splitter);
  233. g_form_inner_container = GUI::Widget::construct(g_right_hand_stack);
  234. g_form_inner_container->set_layout(make<GUI::HorizontalBoxLayout>());
  235. auto form_widgets_toolbar = GUI::ToolBar::construct(Orientation::Vertical, 26, g_form_inner_container);
  236. form_widgets_toolbar->set_preferred_size(38, 0);
  237. GUI::ActionGroup tool_actions;
  238. tool_actions.set_exclusive(true);
  239. auto cursor_tool_action = GUI::Action::create("Cursor", Gfx::Bitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) {
  240. g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget));
  241. });
  242. cursor_tool_action->set_checkable(true);
  243. cursor_tool_action->set_checked(true);
  244. tool_actions.add_action(cursor_tool_action);
  245. form_widgets_toolbar->add_action(cursor_tool_action);
  246. GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& reg) {
  247. auto icon_path = String::format("/res/icons/widgets/%s.png", reg.class_name().characters());
  248. auto action = GUI::Action::create(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg](auto&) {
  249. g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg));
  250. auto widget = reg.construct(&g_form_editor_widget->form_widget());
  251. widget->set_relative_rect(30, 30, 30, 30);
  252. g_form_editor_widget->model().update();
  253. });
  254. action->set_checkable(true);
  255. action->set_checked(false);
  256. tool_actions.add_action(action);
  257. form_widgets_toolbar->add_action(move(action));
  258. });
  259. auto form_editor_inner_splitter = GUI::HorizontalSplitter::construct(g_form_inner_container);
  260. g_form_editor_widget = FormEditorWidget::construct(form_editor_inner_splitter);
  261. auto form_editing_pane_container = GUI::VerticalSplitter::construct(form_editor_inner_splitter);
  262. form_editing_pane_container->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  263. form_editing_pane_container->set_preferred_size(190, 0);
  264. form_editing_pane_container->set_layout(make<GUI::VerticalBoxLayout>());
  265. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  266. auto wrapper = GUI::Widget::construct(form_editing_pane_container.ptr());
  267. wrapper->set_layout(make<GUI::VerticalBoxLayout>());
  268. auto label = GUI::Label::construct(text, wrapper);
  269. label->set_fill_with_background_color(true);
  270. label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  271. label->set_font(Gfx::Font::default_bold_font());
  272. label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  273. label->set_preferred_size(0, 16);
  274. wrapper->add_child(pane_widget);
  275. };
  276. auto form_widget_tree_view = GUI::TreeView::construct(nullptr);
  277. form_widget_tree_view->set_model(g_form_editor_widget->model());
  278. form_widget_tree_view->on_selection_change = [&] {
  279. g_form_editor_widget->selection().disable_hooks();
  280. g_form_editor_widget->selection().clear();
  281. form_widget_tree_view->selection().for_each_index([&](auto& index) {
  282. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  283. // since that would allow you to drag-move the FormWidget.
  284. if (index.internal_data() != &g_form_editor_widget->form_widget())
  285. g_form_editor_widget->selection().add(*(GUI::Widget*)index.internal_data());
  286. });
  287. g_form_editor_widget->update();
  288. g_form_editor_widget->selection().enable_hooks();
  289. };
  290. g_form_editor_widget->selection().on_add = [&](auto& widget) {
  291. form_widget_tree_view->selection().add(g_form_editor_widget->model().index_for_widget(widget));
  292. };
  293. g_form_editor_widget->selection().on_remove = [&](auto& widget) {
  294. form_widget_tree_view->selection().remove(g_form_editor_widget->model().index_for_widget(widget));
  295. };
  296. g_form_editor_widget->selection().on_clear = [&] {
  297. form_widget_tree_view->selection().clear();
  298. };
  299. add_properties_pane("Form widget tree:", form_widget_tree_view);
  300. add_properties_pane("Widget properties:", GUI::TableView::construct(nullptr));
  301. g_text_inner_splitter = GUI::VerticalSplitter::construct(g_right_hand_stack);
  302. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  303. add_new_editor(*g_text_inner_splitter);
  304. auto switch_to_next_editor = GUI::Action::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  305. if (g_all_editor_wrappers.size() <= 1)
  306. return;
  307. Vector<EditorWrapper*> wrappers;
  308. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  309. wrappers.append(&child);
  310. return IterationDecision::Continue;
  311. });
  312. for (int i = 0; i < wrappers.size(); ++i) {
  313. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  314. if (i == wrappers.size() - 1)
  315. wrappers[0]->editor().set_focus(true);
  316. else
  317. wrappers[i + 1]->editor().set_focus(true);
  318. }
  319. }
  320. });
  321. auto switch_to_previous_editor = GUI::Action::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  322. if (g_all_editor_wrappers.size() <= 1)
  323. return;
  324. Vector<EditorWrapper*> wrappers;
  325. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  326. wrappers.append(&child);
  327. return IterationDecision::Continue;
  328. });
  329. for (int i = wrappers.size() - 1; i >= 0; --i) {
  330. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  331. if (i == 0)
  332. wrappers.last()->editor().set_focus(true);
  333. else
  334. wrappers[i - 1]->editor().set_focus(true);
  335. }
  336. }
  337. });
  338. auto remove_current_editor_action = GUI::Action::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  339. if (g_all_editor_wrappers.size() <= 1)
  340. return;
  341. auto wrapper = g_current_editor_wrapper;
  342. switch_to_next_editor->activate();
  343. g_text_inner_splitter->remove_child(*wrapper);
  344. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  345. update_actions();
  346. });
  347. auto save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  348. if (g_currently_open_file.is_empty())
  349. return;
  350. current_editor().write_to_file(g_currently_open_file);
  351. });
  352. toolbar->add_action(new_action);
  353. toolbar->add_action(add_existing_file_action);
  354. toolbar->add_action(save_action);
  355. toolbar->add_action(delete_action);
  356. toolbar->add_separator();
  357. toolbar->add_action(GUI::CommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  358. toolbar->add_action(GUI::CommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  359. toolbar->add_action(GUI::CommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  360. toolbar->add_separator();
  361. toolbar->add_action(GUI::CommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  362. toolbar->add_action(GUI::CommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  363. toolbar->add_separator();
  364. g_project_tree_view->on_activation = [&](auto& index) {
  365. auto filename = g_project_tree_view->model()->data(index, GUI::Model::Role::Custom).to_string();
  366. open_file(filename);
  367. };
  368. s_action_tab_widget = GUI::TabWidget::construct(g_text_inner_splitter);
  369. s_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  370. s_action_tab_widget->set_preferred_size(0, 24);
  371. auto reveal_action_tab = [&](auto& widget) {
  372. if (s_action_tab_widget->preferred_size().height() < 200)
  373. s_action_tab_widget->set_preferred_size(0, 200);
  374. s_action_tab_widget->set_active_widget(widget);
  375. };
  376. auto hide_action_tabs = [&] {
  377. s_action_tab_widget->set_preferred_size(0, 24);
  378. };
  379. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  380. hide_action_tabs();
  381. });
  382. auto add_editor_action = GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E }, [&](auto&) {
  383. add_new_editor(*g_text_inner_splitter);
  384. update_actions();
  385. });
  386. auto find_in_files_widget = FindInFilesWidget::construct(nullptr);
  387. s_action_tab_widget->add_widget("Find in files", find_in_files_widget);
  388. auto terminal_wrapper = TerminalWrapper::construct(nullptr);
  389. s_action_tab_widget->add_widget("Console", terminal_wrapper);
  390. auto locator = Locator::construct(widget);
  391. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  392. locator->open();
  393. });
  394. auto menubar = make<GUI::MenuBar>();
  395. auto app_menu = GUI::Menu::construct("HackStudio");
  396. app_menu->add_action(save_action);
  397. app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  398. app.quit();
  399. }));
  400. menubar->add_menu(move(app_menu));
  401. auto project_menu = GUI::Menu::construct("Project");
  402. project_menu->add_action(new_action);
  403. project_menu->add_action(add_existing_file_action);
  404. menubar->add_menu(move(project_menu));
  405. auto edit_menu = GUI::Menu::construct("Edit");
  406. edit_menu->add_action(GUI::Action::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  407. reveal_action_tab(find_in_files_widget);
  408. find_in_files_widget->focus_textbox_and_select_all();
  409. }));
  410. menubar->add_menu(move(edit_menu));
  411. auto stop_action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png"), [&](auto&) {
  412. terminal_wrapper->kill_running_command();
  413. });
  414. stop_action->set_enabled(false);
  415. terminal_wrapper->on_command_exit = [&] {
  416. stop_action->set_enabled(false);
  417. };
  418. auto build_action = GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  419. reveal_action_tab(terminal_wrapper);
  420. build(terminal_wrapper);
  421. stop_action->set_enabled(true);
  422. });
  423. toolbar->add_action(build_action);
  424. auto run_action = GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/play.png"), [&](auto&) {
  425. reveal_action_tab(terminal_wrapper);
  426. run(terminal_wrapper);
  427. stop_action->set_enabled(true);
  428. });
  429. toolbar->add_action(run_action);
  430. toolbar->add_action(stop_action);
  431. auto build_menu = GUI::Menu::construct("Build");
  432. build_menu->add_action(build_action);
  433. build_menu->add_action(run_action);
  434. build_menu->add_action(stop_action);
  435. menubar->add_menu(move(build_menu));
  436. auto view_menu = GUI::Menu::construct("View");
  437. view_menu->add_action(hide_action_tabs_action);
  438. view_menu->add_action(open_locator_action);
  439. view_menu->add_separator();
  440. view_menu->add_action(add_editor_action);
  441. view_menu->add_action(remove_current_editor_action);
  442. menubar->add_menu(move(view_menu));
  443. auto small_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  444. auto help_menu = GUI::Menu::construct("Help");
  445. help_menu->add_action(GUI::Action::create("About", [&](auto&) {
  446. GUI::AboutDialog::show("HackStudio", small_icon, g_window);
  447. }));
  448. menubar->add_menu(move(help_menu));
  449. app.set_menubar(move(menubar));
  450. g_window->set_icon(small_icon);
  451. g_window->show();
  452. update_actions = [&]() {
  453. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  454. };
  455. open_file("main.cpp");
  456. update_actions();
  457. return app.exec();
  458. }
  459. void build(TerminalWrapper& wrapper)
  460. {
  461. wrapper.run_command("make");
  462. }
  463. void run(TerminalWrapper& wrapper)
  464. {
  465. wrapper.run_command("make run");
  466. }
  467. struct TextStyle {
  468. Color color;
  469. const Gfx::Font* font { nullptr };
  470. };
  471. static TextStyle style_for_token_type(CppToken::Type type)
  472. {
  473. switch (type) {
  474. case CppToken::Type::Keyword:
  475. return { Color::Black, &Gfx::Font::default_bold_fixed_width_font() };
  476. case CppToken::Type::KnownType:
  477. return { Color::from_rgb(0x929200), &Gfx::Font::default_bold_fixed_width_font() };
  478. case CppToken::Type::Identifier:
  479. return { Color::from_rgb(0x000092) };
  480. case CppToken::Type::DoubleQuotedString:
  481. case CppToken::Type::SingleQuotedString:
  482. case CppToken::Type::Number:
  483. return { Color::from_rgb(0x920000) };
  484. case CppToken::Type::PreprocessorStatement:
  485. return { Color::from_rgb(0x009292) };
  486. case CppToken::Type::Comment:
  487. return { Color::from_rgb(0x009200) };
  488. default:
  489. return { Color::Black };
  490. }
  491. }
  492. static void rehighlight()
  493. {
  494. auto text = current_editor().text();
  495. CppLexer lexer(text);
  496. auto tokens = lexer.lex();
  497. Vector<GUI::TextDocumentSpan> spans;
  498. for (auto& token : tokens) {
  499. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  500. dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
  501. #endif
  502. GUI::TextDocumentSpan span;
  503. span.range.set_start({ token.m_start.line, token.m_start.column });
  504. span.range.set_end({ token.m_end.line, token.m_end.column });
  505. auto style = style_for_token_type(token.m_type);
  506. span.color = style.color;
  507. span.font = style.font;
  508. span.is_skippable = token.m_type == CppToken::Type::Whitespace;
  509. span.data = (void*)token.m_type;
  510. spans.append(span);
  511. }
  512. current_editor().document().set_spans(spans);
  513. static_cast<Editor&>(current_editor()).notify_did_rehighlight();
  514. current_editor().update();
  515. }
  516. void open_file(const String& filename)
  517. {
  518. auto file = g_project->get_file(filename);
  519. current_editor().set_document(const_cast<GUI::TextDocument&>(file->document()));
  520. if (filename.ends_with(".cpp") || filename.ends_with(".h")) {
  521. current_editor().on_change = [] { rehighlight(); };
  522. rehighlight();
  523. } else {
  524. current_editor().on_change = nullptr;
  525. }
  526. if (filename.ends_with(".frm")) {
  527. set_edit_mode(EditMode::Form);
  528. } else {
  529. set_edit_mode(EditMode::Text);
  530. }
  531. g_currently_open_file = filename;
  532. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  533. g_project_tree_view->update();
  534. current_editor_wrapper().filename_label().set_text(filename);
  535. current_editor().set_focus(true);
  536. }
  537. bool make_is_available()
  538. {
  539. auto pid = fork();
  540. if (pid < 0)
  541. return false;
  542. if (!pid) {
  543. int rc = execlp("make", "make", "--version", nullptr);
  544. ASSERT(rc < 0);
  545. perror("execl");
  546. exit(127);
  547. }
  548. int wstatus;
  549. waitpid(pid, &wstatus, 0);
  550. return WEXITSTATUS(wstatus) == 0;
  551. }