main.cpp 24 KB

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