main.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. 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();
  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 = g_window->set_main_widget<GUI::Widget>();
  129. widget.set_fill_with_background_color(true);
  130. widget.set_layout<GUI::VerticalBoxLayout>();
  131. widget.layout()->set_spacing(0);
  132. StringBuilder path;
  133. path.append(getenv("PATH"));
  134. if (path.length())
  135. path.append(":");
  136. path.append("/bin:/usr/bin:/usr/local/bin");
  137. setenv("PATH", path.to_string().characters(), true);
  138. if (!make_is_available())
  139. 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);
  140. if (chdir("/home/anon/little") < 0) {
  141. perror("chdir");
  142. return 1;
  143. }
  144. g_project = Project::load_from_file("little.files");
  145. ASSERT(g_project);
  146. auto toolbar = widget.add<GUI::ToolBar>();
  147. auto selected_file_names = [&] {
  148. Vector<String> files;
  149. g_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  150. files.append(g_project->model().data(index).as_string());
  151. });
  152. return files;
  153. };
  154. 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&) {
  155. auto input_box = g_window->add<GUI::InputBox>("Enter name of new file:", "Add new file to project");
  156. if (input_box->exec() == GUI::InputBox::ExecCancel)
  157. return;
  158. auto filename = input_box->text_value();
  159. auto file = Core::File::construct(filename);
  160. if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
  161. GUI::MessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  162. return;
  163. }
  164. if (!g_project->add_file(filename)) {
  165. GUI::MessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  166. // FIXME: Should we unlink the file here maybe?
  167. return;
  168. }
  169. open_file(filename);
  170. });
  171. auto add_existing_file_action = GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  172. auto result = GUI::FilePicker::get_open_filepath("Add existing file to project");
  173. if (!result.has_value())
  174. return;
  175. auto& filename = result.value();
  176. if (!g_project->add_file(filename)) {
  177. GUI::MessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  178. return;
  179. }
  180. open_file(filename);
  181. });
  182. auto delete_action = GUI::CommonActions::make_delete_action([&](const GUI::Action& action) {
  183. (void)action;
  184. auto files = selected_file_names();
  185. if (files.is_empty())
  186. return;
  187. String message;
  188. if (files.size() == 1) {
  189. message = String::format("Really remove %s from the project?", FileSystemPath(files[0]).basename().characters());
  190. } else {
  191. message = String::format("Really remove %d files from the project?", files.size());
  192. }
  193. auto result = GUI::MessageBox::show(
  194. message,
  195. "Confirm deletion",
  196. GUI::MessageBox::Type::Warning,
  197. GUI::MessageBox::InputType::OKCancel,
  198. g_window);
  199. if (result == GUI::MessageBox::ExecCancel)
  200. return;
  201. for (auto& file : files) {
  202. if (!g_project->remove_file(file)) {
  203. GUI::MessageBox::show(
  204. String::format("Removing file %s from the project failed.", file.characters()),
  205. "Removal failed",
  206. GUI::MessageBox::Type::Error,
  207. GUI::MessageBox::InputType::OK,
  208. g_window);
  209. break;
  210. }
  211. }
  212. });
  213. delete_action->set_enabled(false);
  214. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  215. project_tree_view_context_menu->add_action(new_action);
  216. project_tree_view_context_menu->add_action(add_existing_file_action);
  217. project_tree_view_context_menu->add_action(delete_action);
  218. auto outer_splitter = widget.add<GUI::HorizontalSplitter>();
  219. g_project_tree_view = outer_splitter->add<GUI::TreeView>();
  220. g_project_tree_view->set_model(g_project->model());
  221. g_project_tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  222. g_project_tree_view->set_preferred_size(140, 0);
  223. g_project_tree_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  224. if (index.is_valid()) {
  225. project_tree_view_context_menu->popup(event.screen_position());
  226. }
  227. };
  228. g_project_tree_view->on_selection_change = [&] {
  229. delete_action->set_enabled(!g_project_tree_view->selection().is_empty());
  230. };
  231. g_right_hand_stack = outer_splitter->add<GUI::StackWidget>();
  232. g_form_inner_container = g_right_hand_stack->add<GUI::Widget>();
  233. g_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
  234. auto form_widgets_toolbar = g_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
  235. form_widgets_toolbar->set_preferred_size(38, 0);
  236. GUI::ActionGroup tool_actions;
  237. tool_actions.set_exclusive(true);
  238. auto cursor_tool_action = GUI::Action::create("Cursor", Gfx::Bitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) {
  239. g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget));
  240. });
  241. cursor_tool_action->set_checkable(true);
  242. cursor_tool_action->set_checked(true);
  243. tool_actions.add_action(cursor_tool_action);
  244. form_widgets_toolbar->add_action(cursor_tool_action);
  245. GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& reg) {
  246. auto icon_path = String::format("/res/icons/widgets/G%s.png", reg.class_name().characters());
  247. auto action = GUI::Action::create(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg](auto&) {
  248. g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg));
  249. auto widget = reg.construct();
  250. g_form_editor_widget->form_widget().add_child(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 = g_form_inner_container->add<GUI::HorizontalSplitter>();
  260. g_form_editor_widget = form_editor_inner_splitter->add<FormEditorWidget>();
  261. auto form_editing_pane_container = form_editor_inner_splitter->add<GUI::VerticalSplitter>();
  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<GUI::VerticalBoxLayout>();
  265. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  266. auto wrapper = form_editing_pane_container->add<GUI::Widget>();
  267. wrapper->set_layout<GUI::VerticalBoxLayout>();
  268. auto label = wrapper->add<GUI::Label>(text);
  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();
  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());
  301. g_text_inner_splitter = g_right_hand_stack->add<GUI::VerticalSplitter>();
  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 (size_t 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 = g_text_inner_splitter->add<GUI::TabWidget>();
  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 = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  387. auto terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Console");
  388. auto locator = widget.add<Locator>();
  389. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  390. locator->open();
  391. });
  392. auto menubar = make<GUI::MenuBar>();
  393. auto app_menu = GUI::Menu::construct("HackStudio");
  394. app_menu->add_action(save_action);
  395. app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  396. app.quit();
  397. }));
  398. menubar->add_menu(move(app_menu));
  399. auto project_menu = GUI::Menu::construct("Project");
  400. project_menu->add_action(new_action);
  401. project_menu->add_action(add_existing_file_action);
  402. menubar->add_menu(move(project_menu));
  403. auto edit_menu = GUI::Menu::construct("Edit");
  404. edit_menu->add_action(GUI::Action::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  405. reveal_action_tab(find_in_files_widget);
  406. find_in_files_widget->focus_textbox_and_select_all();
  407. }));
  408. menubar->add_menu(move(edit_menu));
  409. auto stop_action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png"), [&](auto&) {
  410. terminal_wrapper->kill_running_command();
  411. });
  412. stop_action->set_enabled(false);
  413. terminal_wrapper->on_command_exit = [&] {
  414. stop_action->set_enabled(false);
  415. };
  416. auto build_action = GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  417. reveal_action_tab(terminal_wrapper);
  418. build(terminal_wrapper);
  419. stop_action->set_enabled(true);
  420. });
  421. toolbar->add_action(build_action);
  422. auto run_action = GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/play.png"), [&](auto&) {
  423. reveal_action_tab(terminal_wrapper);
  424. run(terminal_wrapper);
  425. stop_action->set_enabled(true);
  426. });
  427. toolbar->add_action(run_action);
  428. toolbar->add_action(stop_action);
  429. auto build_menu = GUI::Menu::construct("Build");
  430. build_menu->add_action(build_action);
  431. build_menu->add_action(run_action);
  432. build_menu->add_action(stop_action);
  433. menubar->add_menu(move(build_menu));
  434. auto view_menu = GUI::Menu::construct("View");
  435. view_menu->add_action(hide_action_tabs_action);
  436. view_menu->add_action(open_locator_action);
  437. view_menu->add_separator();
  438. view_menu->add_action(add_editor_action);
  439. view_menu->add_action(remove_current_editor_action);
  440. menubar->add_menu(move(view_menu));
  441. auto small_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  442. auto help_menu = GUI::Menu::construct("Help");
  443. help_menu->add_action(GUI::Action::create("About", [&](auto&) {
  444. GUI::AboutDialog::show("HackStudio", small_icon, g_window);
  445. }));
  446. menubar->add_menu(move(help_menu));
  447. app.set_menubar(move(menubar));
  448. g_window->set_icon(small_icon);
  449. g_window->show();
  450. update_actions = [&]() {
  451. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  452. };
  453. open_file("main.cpp");
  454. update_actions();
  455. return app.exec();
  456. }
  457. void build(TerminalWrapper& wrapper)
  458. {
  459. wrapper.run_command("make");
  460. }
  461. void run(TerminalWrapper& wrapper)
  462. {
  463. wrapper.run_command("make run");
  464. }
  465. void open_file(const String& filename)
  466. {
  467. auto file = g_project->get_file(filename);
  468. current_editor().set_document(const_cast<GUI::TextDocument&>(file->document()));
  469. if (filename.ends_with(".cpp") || filename.ends_with(".h"))
  470. current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
  471. if (filename.ends_with(".frm")) {
  472. set_edit_mode(EditMode::Form);
  473. } else {
  474. set_edit_mode(EditMode::Text);
  475. }
  476. g_currently_open_file = filename;
  477. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  478. g_project_tree_view->update();
  479. current_editor_wrapper().filename_label().set_text(filename);
  480. current_editor().set_focus(true);
  481. }
  482. bool make_is_available()
  483. {
  484. auto pid = fork();
  485. if (pid < 0)
  486. return false;
  487. if (!pid) {
  488. int rc = execlp("make", "make", "--version", nullptr);
  489. ASSERT(rc < 0);
  490. perror("execl");
  491. exit(127);
  492. }
  493. int wstatus;
  494. waitpid(pid, &wstatus, 0);
  495. return WEXITSTATUS(wstatus) == 0;
  496. }