main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 "CursorTool.h"
  27. #include "Editor.h"
  28. #include "EditorWrapper.h"
  29. #include "FindInFilesWidget.h"
  30. #include "FormEditorWidget.h"
  31. #include "FormWidget.h"
  32. #include "Locator.h"
  33. #include "Project.h"
  34. #include "TerminalWrapper.h"
  35. #include "WidgetTool.h"
  36. #include "WidgetTreeModel.h"
  37. #include <AK/StringBuilder.h>
  38. #include <LibCore/File.h>
  39. #include <LibGUI/AboutDialog.h>
  40. #include <LibGUI/Action.h>
  41. #include <LibGUI/ActionGroup.h>
  42. #include <LibGUI/Application.h>
  43. #include <LibGUI/BoxLayout.h>
  44. #include <LibGUI/Button.h>
  45. #include <LibGUI/CppSyntaxHighlighter.h>
  46. #include <LibGUI/FilePicker.h>
  47. #include <LibGUI/InputBox.h>
  48. #include <LibGUI/JSSyntaxHighlighter.h>
  49. #include <LibGUI/Label.h>
  50. #include <LibGUI/Menu.h>
  51. #include <LibGUI/MenuBar.h>
  52. #include <LibGUI/MessageBox.h>
  53. #include <LibGUI/Splitter.h>
  54. #include <LibGUI/StackWidget.h>
  55. #include <LibGUI/TabWidget.h>
  56. #include <LibGUI/TableView.h>
  57. #include <LibGUI/TextBox.h>
  58. #include <LibGUI/TextEditor.h>
  59. #include <LibGUI/ToolBar.h>
  60. #include <LibGUI/TreeView.h>
  61. #include <LibGUI/Widget.h>
  62. #include <LibGUI/Window.h>
  63. #include <stdio.h>
  64. #include <sys/wait.h>
  65. #include <unistd.h>
  66. NonnullRefPtrVector<EditorWrapper> g_all_editor_wrappers;
  67. RefPtr<EditorWrapper> g_current_editor_wrapper;
  68. Function<void(String)> g_open_file;
  69. String g_currently_open_file;
  70. OwnPtr<Project> g_project;
  71. RefPtr<GUI::Window> g_window;
  72. RefPtr<GUI::TreeView> g_project_tree_view;
  73. RefPtr<GUI::StackWidget> g_right_hand_stack;
  74. RefPtr<GUI::Splitter> g_text_inner_splitter;
  75. RefPtr<GUI::Widget> g_form_inner_container;
  76. RefPtr<FormEditorWidget> g_form_editor_widget;
  77. static RefPtr<GUI::TabWidget> s_action_tab_widget;
  78. void add_new_editor(GUI::Widget& parent)
  79. {
  80. auto wrapper = EditorWrapper::construct();
  81. if (s_action_tab_widget) {
  82. parent.insert_child_before(wrapper, *s_action_tab_widget);
  83. } else {
  84. parent.add_child(wrapper);
  85. }
  86. g_current_editor_wrapper = wrapper;
  87. g_all_editor_wrappers.append(wrapper);
  88. wrapper->editor().set_focus(true);
  89. }
  90. enum class EditMode {
  91. Text,
  92. Form,
  93. };
  94. void set_edit_mode(EditMode mode)
  95. {
  96. if (mode == EditMode::Text) {
  97. g_right_hand_stack->set_active_widget(g_text_inner_splitter);
  98. } else if (mode == EditMode::Form) {
  99. g_right_hand_stack->set_active_widget(g_form_inner_container);
  100. }
  101. }
  102. EditorWrapper& current_editor_wrapper()
  103. {
  104. ASSERT(g_current_editor_wrapper);
  105. return *g_current_editor_wrapper;
  106. }
  107. Editor& current_editor()
  108. {
  109. return current_editor_wrapper().editor();
  110. }
  111. static void build(TerminalWrapper&);
  112. static void run(TerminalWrapper&);
  113. void open_project(String);
  114. void open_file(const String&);
  115. bool make_is_available();
  116. int main(int argc, char** argv)
  117. {
  118. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec unix fattr", nullptr) < 0) {
  119. perror("pledge");
  120. return 1;
  121. }
  122. GUI::Application app(argc, argv);
  123. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec fattr", nullptr) < 0) {
  124. perror("pledge");
  125. return 1;
  126. }
  127. Function<void()> update_actions;
  128. g_window = GUI::Window::construct();
  129. g_window->set_rect(90, 90, 840, 600);
  130. g_window->set_title("HackStudio");
  131. auto& widget = g_window->set_main_widget<GUI::Widget>();
  132. widget.set_fill_with_background_color(true);
  133. widget.set_layout<GUI::VerticalBoxLayout>();
  134. widget.layout()->set_spacing(0);
  135. StringBuilder path;
  136. path.append(getenv("PATH"));
  137. if (path.length())
  138. path.append(":");
  139. path.append("/bin:/usr/bin:/usr/local/bin");
  140. setenv("PATH", path.to_string().characters(), true);
  141. if (!make_is_available())
  142. 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);
  143. open_project("/home/anon/js/javascript.files");
  144. auto& toolbar = widget.add<GUI::ToolBar>();
  145. auto selected_file_names = [&] {
  146. Vector<String> files;
  147. g_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  148. files.append(g_project->model().data(index).as_string());
  149. });
  150. return files;
  151. };
  152. 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&) {
  153. auto input_box = GUI::InputBox::construct("Enter name of new file:", "Add new file to project", g_window);
  154. if (input_box->exec() == GUI::InputBox::ExecCancel)
  155. return;
  156. auto filename = input_box->text_value();
  157. auto file = Core::File::construct(filename);
  158. if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
  159. GUI::MessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  160. return;
  161. }
  162. if (!g_project->add_file(filename)) {
  163. GUI::MessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  164. // FIXME: Should we unlink the file here maybe?
  165. return;
  166. }
  167. open_file(filename);
  168. });
  169. auto add_existing_file_action = GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  170. auto result = GUI::FilePicker::get_open_filepath("Add existing file to project");
  171. if (!result.has_value())
  172. return;
  173. auto& filename = result.value();
  174. if (!g_project->add_file(filename)) {
  175. GUI::MessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  176. return;
  177. }
  178. open_file(filename);
  179. });
  180. auto delete_action = GUI::CommonActions::make_delete_action([&](const GUI::Action& action) {
  181. (void)action;
  182. auto files = selected_file_names();
  183. if (files.is_empty())
  184. return;
  185. String message;
  186. if (files.size() == 1) {
  187. message = String::format("Really remove %s from the project?", FileSystemPath(files[0]).basename().characters());
  188. } else {
  189. message = String::format("Really remove %d files from the project?", files.size());
  190. }
  191. auto result = GUI::MessageBox::show(
  192. message,
  193. "Confirm deletion",
  194. GUI::MessageBox::Type::Warning,
  195. GUI::MessageBox::InputType::OKCancel,
  196. g_window);
  197. if (result == GUI::MessageBox::ExecCancel)
  198. return;
  199. for (auto& file : files) {
  200. if (!g_project->remove_file(file)) {
  201. GUI::MessageBox::show(
  202. String::format("Removing file %s from the project failed.", file.characters()),
  203. "Removal failed",
  204. GUI::MessageBox::Type::Error,
  205. GUI::MessageBox::InputType::OK,
  206. g_window);
  207. break;
  208. }
  209. }
  210. });
  211. delete_action->set_enabled(false);
  212. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  213. project_tree_view_context_menu->add_action(new_action);
  214. project_tree_view_context_menu->add_action(add_existing_file_action);
  215. project_tree_view_context_menu->add_action(delete_action);
  216. auto& outer_splitter = widget.add<GUI::HorizontalSplitter>();
  217. g_project_tree_view = outer_splitter.add<GUI::TreeView>();
  218. g_project_tree_view->set_model(g_project->model());
  219. g_project_tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  220. g_project_tree_view->set_preferred_size(140, 0);
  221. g_project_tree_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  222. if (index.is_valid()) {
  223. project_tree_view_context_menu->popup(event.screen_position());
  224. }
  225. };
  226. g_project_tree_view->on_selection_change = [&] {
  227. delete_action->set_enabled(!g_project_tree_view->selection().is_empty());
  228. };
  229. g_right_hand_stack = outer_splitter.add<GUI::StackWidget>();
  230. g_form_inner_container = g_right_hand_stack->add<GUI::Widget>();
  231. g_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
  232. auto& form_widgets_toolbar = g_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
  233. form_widgets_toolbar.set_preferred_size(38, 0);
  234. GUI::ActionGroup tool_actions;
  235. tool_actions.set_exclusive(true);
  236. auto cursor_tool_action = GUI::Action::create("Cursor", Gfx::Bitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) {
  237. g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget));
  238. });
  239. cursor_tool_action->set_checkable(true);
  240. cursor_tool_action->set_checked(true);
  241. tool_actions.add_action(cursor_tool_action);
  242. form_widgets_toolbar.add_action(cursor_tool_action);
  243. GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& reg) {
  244. auto icon_path = String::format("/res/icons/widgets/G%s.png", reg.class_name().characters());
  245. auto action = GUI::Action::create(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg](auto&) {
  246. g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg));
  247. auto widget = reg.construct();
  248. g_form_editor_widget->form_widget().add_child(widget);
  249. widget->set_relative_rect(30, 30, 30, 30);
  250. g_form_editor_widget->model().update();
  251. });
  252. action->set_checkable(true);
  253. action->set_checked(false);
  254. tool_actions.add_action(action);
  255. form_widgets_toolbar.add_action(move(action));
  256. });
  257. auto& form_editor_inner_splitter = g_form_inner_container->add<GUI::HorizontalSplitter>();
  258. g_form_editor_widget = form_editor_inner_splitter.add<FormEditorWidget>();
  259. auto& form_editing_pane_container = form_editor_inner_splitter.add<GUI::VerticalSplitter>();
  260. form_editing_pane_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  261. form_editing_pane_container.set_preferred_size(190, 0);
  262. form_editing_pane_container.set_layout<GUI::VerticalBoxLayout>();
  263. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  264. auto& wrapper = form_editing_pane_container.add<GUI::Widget>();
  265. wrapper.set_layout<GUI::VerticalBoxLayout>();
  266. auto& label = wrapper.add<GUI::Label>(text);
  267. label.set_fill_with_background_color(true);
  268. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  269. label.set_font(Gfx::Font::default_bold_font());
  270. label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  271. label.set_preferred_size(0, 16);
  272. wrapper.add_child(pane_widget);
  273. };
  274. auto form_widget_tree_view = GUI::TreeView::construct();
  275. form_widget_tree_view->set_model(g_form_editor_widget->model());
  276. form_widget_tree_view->on_selection_change = [&] {
  277. g_form_editor_widget->selection().disable_hooks();
  278. g_form_editor_widget->selection().clear();
  279. form_widget_tree_view->selection().for_each_index([&](auto& index) {
  280. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  281. // since that would allow you to drag-move the FormWidget.
  282. if (index.internal_data() != &g_form_editor_widget->form_widget())
  283. g_form_editor_widget->selection().add(*(GUI::Widget*)index.internal_data());
  284. });
  285. g_form_editor_widget->update();
  286. g_form_editor_widget->selection().enable_hooks();
  287. };
  288. g_form_editor_widget->selection().on_add = [&](auto& widget) {
  289. form_widget_tree_view->selection().add(g_form_editor_widget->model().index_for_widget(widget));
  290. };
  291. g_form_editor_widget->selection().on_remove = [&](auto& widget) {
  292. form_widget_tree_view->selection().remove(g_form_editor_widget->model().index_for_widget(widget));
  293. };
  294. g_form_editor_widget->selection().on_clear = [&] {
  295. form_widget_tree_view->selection().clear();
  296. };
  297. add_properties_pane("Form widget tree:", form_widget_tree_view);
  298. add_properties_pane("Widget properties:", GUI::TableView::construct());
  299. g_text_inner_splitter = g_right_hand_stack->add<GUI::VerticalSplitter>();
  300. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  301. add_new_editor(*g_text_inner_splitter);
  302. auto switch_to_next_editor = GUI::Action::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  303. if (g_all_editor_wrappers.size() <= 1)
  304. return;
  305. Vector<EditorWrapper*> wrappers;
  306. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  307. wrappers.append(&child);
  308. return IterationDecision::Continue;
  309. });
  310. for (size_t i = 0; i < wrappers.size(); ++i) {
  311. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  312. if (i == wrappers.size() - 1)
  313. wrappers[0]->editor().set_focus(true);
  314. else
  315. wrappers[i + 1]->editor().set_focus(true);
  316. }
  317. }
  318. });
  319. auto switch_to_previous_editor = GUI::Action::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  320. if (g_all_editor_wrappers.size() <= 1)
  321. return;
  322. Vector<EditorWrapper*> wrappers;
  323. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  324. wrappers.append(&child);
  325. return IterationDecision::Continue;
  326. });
  327. for (int i = wrappers.size() - 1; i >= 0; --i) {
  328. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  329. if (i == 0)
  330. wrappers.last()->editor().set_focus(true);
  331. else
  332. wrappers[i - 1]->editor().set_focus(true);
  333. }
  334. }
  335. });
  336. auto remove_current_editor_action = GUI::Action::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  337. if (g_all_editor_wrappers.size() <= 1)
  338. return;
  339. auto wrapper = g_current_editor_wrapper;
  340. switch_to_next_editor->activate();
  341. g_text_inner_splitter->remove_child(*wrapper);
  342. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  343. update_actions();
  344. });
  345. auto open_action = GUI::Action::create("Open Project", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  346. auto open_path = GUI::FilePicker::get_open_filepath();
  347. if (!open_path.has_value())
  348. return;
  349. open_project(open_path.value());
  350. open_file(g_project->default_file());
  351. update_actions();
  352. });
  353. auto save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  354. if (g_currently_open_file.is_empty())
  355. return;
  356. current_editor().write_to_file(g_currently_open_file);
  357. });
  358. toolbar.add_action(new_action);
  359. toolbar.add_action(add_existing_file_action);
  360. toolbar.add_action(save_action);
  361. toolbar.add_action(delete_action);
  362. toolbar.add_separator();
  363. toolbar.add_action(GUI::CommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  364. toolbar.add_action(GUI::CommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  365. toolbar.add_action(GUI::CommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  366. toolbar.add_separator();
  367. toolbar.add_action(GUI::CommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  368. toolbar.add_action(GUI::CommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  369. toolbar.add_separator();
  370. g_project_tree_view->on_activation = [&](auto& index) {
  371. auto filename = g_project_tree_view->model()->data(index, GUI::Model::Role::Custom).to_string();
  372. open_file(filename);
  373. };
  374. s_action_tab_widget = g_text_inner_splitter->add<GUI::TabWidget>();
  375. s_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  376. s_action_tab_widget->set_preferred_size(0, 24);
  377. auto reveal_action_tab = [&](auto& widget) {
  378. if (s_action_tab_widget->preferred_size().height() < 200)
  379. s_action_tab_widget->set_preferred_size(0, 200);
  380. s_action_tab_widget->set_active_widget(widget);
  381. };
  382. auto hide_action_tabs = [&] {
  383. s_action_tab_widget->set_preferred_size(0, 24);
  384. };
  385. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  386. hide_action_tabs();
  387. });
  388. auto add_editor_action = GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E }, [&](auto&) {
  389. add_new_editor(*g_text_inner_splitter);
  390. update_actions();
  391. });
  392. auto find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  393. auto terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Console");
  394. auto& locator = widget.add<Locator>();
  395. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  396. locator.open();
  397. });
  398. auto menubar = make<GUI::MenuBar>();
  399. auto app_menu = GUI::Menu::construct("HackStudio");
  400. app_menu->add_action(open_action);
  401. app_menu->add_action(save_action);
  402. app_menu->add_separator();
  403. app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  404. app.quit();
  405. }));
  406. menubar->add_menu(move(app_menu));
  407. auto project_menu = GUI::Menu::construct("Project");
  408. project_menu->add_action(new_action);
  409. project_menu->add_action(add_existing_file_action);
  410. menubar->add_menu(move(project_menu));
  411. auto edit_menu = GUI::Menu::construct("Edit");
  412. edit_menu->add_action(GUI::Action::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  413. reveal_action_tab(find_in_files_widget);
  414. find_in_files_widget->focus_textbox_and_select_all();
  415. }));
  416. menubar->add_menu(move(edit_menu));
  417. auto stop_action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png"), [&](auto&) {
  418. terminal_wrapper->kill_running_command();
  419. });
  420. stop_action->set_enabled(false);
  421. terminal_wrapper->on_command_exit = [&] {
  422. stop_action->set_enabled(false);
  423. };
  424. auto build_action = GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  425. reveal_action_tab(terminal_wrapper);
  426. build(terminal_wrapper);
  427. stop_action->set_enabled(true);
  428. });
  429. toolbar.add_action(build_action);
  430. auto run_action = GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/play.png"), [&](auto&) {
  431. reveal_action_tab(terminal_wrapper);
  432. run(terminal_wrapper);
  433. stop_action->set_enabled(true);
  434. });
  435. toolbar.add_action(run_action);
  436. toolbar.add_action(stop_action);
  437. auto build_menu = GUI::Menu::construct("Build");
  438. build_menu->add_action(build_action);
  439. build_menu->add_action(run_action);
  440. build_menu->add_action(stop_action);
  441. menubar->add_menu(move(build_menu));
  442. auto view_menu = GUI::Menu::construct("View");
  443. view_menu->add_action(hide_action_tabs_action);
  444. view_menu->add_action(open_locator_action);
  445. view_menu->add_separator();
  446. view_menu->add_action(add_editor_action);
  447. view_menu->add_action(remove_current_editor_action);
  448. menubar->add_menu(move(view_menu));
  449. auto small_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  450. auto help_menu = GUI::Menu::construct("Help");
  451. help_menu->add_action(GUI::Action::create("About", [&](auto&) {
  452. GUI::AboutDialog::show("HackStudio", small_icon, g_window);
  453. }));
  454. menubar->add_menu(move(help_menu));
  455. app.set_menubar(move(menubar));
  456. g_window->set_icon(small_icon);
  457. g_window->show();
  458. update_actions = [&]() {
  459. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  460. };
  461. g_open_file = open_file;
  462. open_file(g_project->default_file());
  463. update_actions();
  464. return app.exec();
  465. }
  466. void build(TerminalWrapper& wrapper)
  467. {
  468. if (g_project->type() == ProjectType::Javascript && g_currently_open_file.ends_with(".js"))
  469. wrapper.run_command(String::format("js -A %s", g_currently_open_file.characters()));
  470. else
  471. wrapper.run_command("make");
  472. }
  473. void run(TerminalWrapper& wrapper)
  474. {
  475. if (g_project->type() == ProjectType::Javascript && g_currently_open_file.ends_with(".js"))
  476. wrapper.run_command(String::format("js %s", g_currently_open_file.characters()));
  477. else
  478. wrapper.run_command("make run");
  479. }
  480. void open_project(String filename)
  481. {
  482. FileSystemPath path(filename);
  483. if (chdir(path.dirname().characters()) < 0) {
  484. perror("chdir");
  485. exit(1);
  486. }
  487. g_project = Project::load_from_file(filename);
  488. ASSERT(g_project);
  489. if (g_project_tree_view) {
  490. g_project_tree_view->set_model(g_project->model());
  491. g_project_tree_view->update();
  492. }
  493. }
  494. void open_file(const String& filename)
  495. {
  496. auto project_file = g_project->get_file(filename);
  497. if (project_file) {
  498. current_editor().set_document(const_cast<GUI::TextDocument&>(project_file->document()));
  499. current_editor().set_readonly(false);
  500. } else {
  501. auto external_file = ProjectFile::construct_with_name(filename);
  502. current_editor().set_document(const_cast<GUI::TextDocument&>(external_file->document()));
  503. current_editor().set_readonly(true);
  504. }
  505. if (filename.ends_with(".cpp") || filename.ends_with(".h"))
  506. current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
  507. else if (filename.ends_with(".js"))
  508. current_editor().set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
  509. else
  510. current_editor().set_syntax_highlighter(nullptr);
  511. if (filename.ends_with(".frm")) {
  512. set_edit_mode(EditMode::Form);
  513. } else {
  514. set_edit_mode(EditMode::Text);
  515. }
  516. g_currently_open_file = filename;
  517. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  518. g_project_tree_view->update();
  519. current_editor_wrapper().filename_label().set_text(filename);
  520. current_editor().set_focus(true);
  521. }
  522. bool make_is_available()
  523. {
  524. auto pid = fork();
  525. if (pid < 0)
  526. return false;
  527. if (!pid) {
  528. int rc = execlp("make", "make", "--version", nullptr);
  529. ASSERT(rc < 0);
  530. perror("execl");
  531. exit(127);
  532. }
  533. int wstatus;
  534. waitpid(pid, &wstatus, 0);
  535. return WEXITSTATUS(wstatus) == 0;
  536. }