main.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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_file(const String&);
  113. bool make_is_available();
  114. int main(int argc, char** argv)
  115. {
  116. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec unix fattr", nullptr) < 0) {
  117. perror("pledge");
  118. return 1;
  119. }
  120. GUI::Application app(argc, argv);
  121. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec fattr", nullptr) < 0) {
  122. perror("pledge");
  123. return 1;
  124. }
  125. Function<void()> update_actions;
  126. g_window = GUI::Window::construct();
  127. g_window->set_rect(90, 90, 840, 600);
  128. g_window->set_title("HackStudio");
  129. auto& widget = g_window->set_main_widget<GUI::Widget>();
  130. widget.set_fill_with_background_color(true);
  131. widget.set_layout<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 = widget.add<GUI::ToolBar>();
  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 = widget.add<GUI::HorizontalSplitter>();
  220. g_project_tree_view = outer_splitter.add<GUI::TreeView>();
  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 = outer_splitter.add<GUI::StackWidget>();
  233. g_form_inner_container = g_right_hand_stack->add<GUI::Widget>();
  234. g_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
  235. auto& form_widgets_toolbar = g_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
  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/G%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();
  251. g_form_editor_widget->form_widget().add_child(widget);
  252. widget->set_relative_rect(30, 30, 30, 30);
  253. g_form_editor_widget->model().update();
  254. });
  255. action->set_checkable(true);
  256. action->set_checked(false);
  257. tool_actions.add_action(action);
  258. form_widgets_toolbar.add_action(move(action));
  259. });
  260. auto& form_editor_inner_splitter = g_form_inner_container->add<GUI::HorizontalSplitter>();
  261. g_form_editor_widget = form_editor_inner_splitter.add<FormEditorWidget>();
  262. auto& form_editing_pane_container = form_editor_inner_splitter.add<GUI::VerticalSplitter>();
  263. form_editing_pane_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  264. form_editing_pane_container.set_preferred_size(190, 0);
  265. form_editing_pane_container.set_layout<GUI::VerticalBoxLayout>();
  266. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  267. auto& wrapper = form_editing_pane_container.add<GUI::Widget>();
  268. wrapper.set_layout<GUI::VerticalBoxLayout>();
  269. auto& label = wrapper.add<GUI::Label>(text);
  270. label.set_fill_with_background_color(true);
  271. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  272. label.set_font(Gfx::Font::default_bold_font());
  273. label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  274. label.set_preferred_size(0, 16);
  275. wrapper.add_child(pane_widget);
  276. };
  277. auto form_widget_tree_view = GUI::TreeView::construct();
  278. form_widget_tree_view->set_model(g_form_editor_widget->model());
  279. form_widget_tree_view->on_selection_change = [&] {
  280. g_form_editor_widget->selection().disable_hooks();
  281. g_form_editor_widget->selection().clear();
  282. form_widget_tree_view->selection().for_each_index([&](auto& index) {
  283. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  284. // since that would allow you to drag-move the FormWidget.
  285. if (index.internal_data() != &g_form_editor_widget->form_widget())
  286. g_form_editor_widget->selection().add(*(GUI::Widget*)index.internal_data());
  287. });
  288. g_form_editor_widget->update();
  289. g_form_editor_widget->selection().enable_hooks();
  290. };
  291. g_form_editor_widget->selection().on_add = [&](auto& widget) {
  292. form_widget_tree_view->selection().add(g_form_editor_widget->model().index_for_widget(widget));
  293. };
  294. g_form_editor_widget->selection().on_remove = [&](auto& widget) {
  295. form_widget_tree_view->selection().remove(g_form_editor_widget->model().index_for_widget(widget));
  296. };
  297. g_form_editor_widget->selection().on_clear = [&] {
  298. form_widget_tree_view->selection().clear();
  299. };
  300. add_properties_pane("Form widget tree:", form_widget_tree_view);
  301. add_properties_pane("Widget properties:", GUI::TableView::construct());
  302. g_text_inner_splitter = g_right_hand_stack->add<GUI::VerticalSplitter>();
  303. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  304. add_new_editor(*g_text_inner_splitter);
  305. auto switch_to_next_editor = GUI::Action::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  306. if (g_all_editor_wrappers.size() <= 1)
  307. return;
  308. Vector<EditorWrapper*> wrappers;
  309. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  310. wrappers.append(&child);
  311. return IterationDecision::Continue;
  312. });
  313. for (size_t i = 0; i < wrappers.size(); ++i) {
  314. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  315. if (i == wrappers.size() - 1)
  316. wrappers[0]->editor().set_focus(true);
  317. else
  318. wrappers[i + 1]->editor().set_focus(true);
  319. }
  320. }
  321. });
  322. auto switch_to_previous_editor = GUI::Action::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  323. if (g_all_editor_wrappers.size() <= 1)
  324. return;
  325. Vector<EditorWrapper*> wrappers;
  326. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  327. wrappers.append(&child);
  328. return IterationDecision::Continue;
  329. });
  330. for (int i = wrappers.size() - 1; i >= 0; --i) {
  331. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  332. if (i == 0)
  333. wrappers.last()->editor().set_focus(true);
  334. else
  335. wrappers[i - 1]->editor().set_focus(true);
  336. }
  337. }
  338. });
  339. auto remove_current_editor_action = GUI::Action::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  340. if (g_all_editor_wrappers.size() <= 1)
  341. return;
  342. auto wrapper = g_current_editor_wrapper;
  343. switch_to_next_editor->activate();
  344. g_text_inner_splitter->remove_child(*wrapper);
  345. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  346. update_actions();
  347. });
  348. auto save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  349. if (g_currently_open_file.is_empty())
  350. return;
  351. current_editor().write_to_file(g_currently_open_file);
  352. });
  353. toolbar.add_action(new_action);
  354. toolbar.add_action(add_existing_file_action);
  355. toolbar.add_action(save_action);
  356. toolbar.add_action(delete_action);
  357. toolbar.add_separator();
  358. toolbar.add_action(GUI::CommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  359. toolbar.add_action(GUI::CommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  360. toolbar.add_action(GUI::CommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  361. toolbar.add_separator();
  362. toolbar.add_action(GUI::CommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  363. toolbar.add_action(GUI::CommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  364. toolbar.add_separator();
  365. g_project_tree_view->on_activation = [&](auto& index) {
  366. auto filename = g_project_tree_view->model()->data(index, GUI::Model::Role::Custom).to_string();
  367. open_file(filename);
  368. };
  369. s_action_tab_widget = g_text_inner_splitter->add<GUI::TabWidget>();
  370. s_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  371. s_action_tab_widget->set_preferred_size(0, 24);
  372. auto reveal_action_tab = [&](auto& widget) {
  373. if (s_action_tab_widget->preferred_size().height() < 200)
  374. s_action_tab_widget->set_preferred_size(0, 200);
  375. s_action_tab_widget->set_active_widget(widget);
  376. };
  377. auto hide_action_tabs = [&] {
  378. s_action_tab_widget->set_preferred_size(0, 24);
  379. };
  380. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  381. hide_action_tabs();
  382. });
  383. auto add_editor_action = GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E }, [&](auto&) {
  384. add_new_editor(*g_text_inner_splitter);
  385. update_actions();
  386. });
  387. auto find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  388. auto terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Console");
  389. auto& locator = widget.add<Locator>();
  390. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  391. locator.open();
  392. });
  393. auto menubar = make<GUI::MenuBar>();
  394. auto app_menu = GUI::Menu::construct("HackStudio");
  395. app_menu->add_action(save_action);
  396. app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  397. app.quit();
  398. }));
  399. menubar->add_menu(move(app_menu));
  400. auto project_menu = GUI::Menu::construct("Project");
  401. project_menu->add_action(new_action);
  402. project_menu->add_action(add_existing_file_action);
  403. menubar->add_menu(move(project_menu));
  404. auto edit_menu = GUI::Menu::construct("Edit");
  405. edit_menu->add_action(GUI::Action::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  406. reveal_action_tab(find_in_files_widget);
  407. find_in_files_widget->focus_textbox_and_select_all();
  408. }));
  409. menubar->add_menu(move(edit_menu));
  410. auto stop_action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png"), [&](auto&) {
  411. terminal_wrapper->kill_running_command();
  412. });
  413. stop_action->set_enabled(false);
  414. terminal_wrapper->on_command_exit = [&] {
  415. stop_action->set_enabled(false);
  416. };
  417. auto build_action = GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  418. reveal_action_tab(terminal_wrapper);
  419. build(terminal_wrapper);
  420. stop_action->set_enabled(true);
  421. });
  422. toolbar.add_action(build_action);
  423. auto run_action = GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/play.png"), [&](auto&) {
  424. reveal_action_tab(terminal_wrapper);
  425. run(terminal_wrapper);
  426. stop_action->set_enabled(true);
  427. });
  428. toolbar.add_action(run_action);
  429. toolbar.add_action(stop_action);
  430. auto build_menu = GUI::Menu::construct("Build");
  431. build_menu->add_action(build_action);
  432. build_menu->add_action(run_action);
  433. build_menu->add_action(stop_action);
  434. menubar->add_menu(move(build_menu));
  435. auto view_menu = GUI::Menu::construct("View");
  436. view_menu->add_action(hide_action_tabs_action);
  437. view_menu->add_action(open_locator_action);
  438. view_menu->add_separator();
  439. view_menu->add_action(add_editor_action);
  440. view_menu->add_action(remove_current_editor_action);
  441. menubar->add_menu(move(view_menu));
  442. auto small_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  443. auto help_menu = GUI::Menu::construct("Help");
  444. help_menu->add_action(GUI::Action::create("About", [&](auto&) {
  445. GUI::AboutDialog::show("HackStudio", small_icon, g_window);
  446. }));
  447. menubar->add_menu(move(help_menu));
  448. app.set_menubar(move(menubar));
  449. g_window->set_icon(small_icon);
  450. g_window->show();
  451. update_actions = [&]() {
  452. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  453. };
  454. g_open_file = open_file;
  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. void open_file(const String& filename)
  468. {
  469. auto project_file = g_project->get_file(filename);
  470. if (project_file) {
  471. current_editor().set_document(const_cast<GUI::TextDocument&>(project_file->document()));
  472. current_editor().set_readonly(false);
  473. } else {
  474. auto external_file = ProjectFile::construct_with_name(filename);
  475. current_editor().set_document(const_cast<GUI::TextDocument&>(external_file->document()));
  476. current_editor().set_readonly(true);
  477. }
  478. if (filename.ends_with(".cpp") || filename.ends_with(".h"))
  479. current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
  480. if (filename.ends_with(".frm")) {
  481. set_edit_mode(EditMode::Form);
  482. } else {
  483. set_edit_mode(EditMode::Text);
  484. }
  485. g_currently_open_file = filename;
  486. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  487. g_project_tree_view->update();
  488. current_editor_wrapper().filename_label().set_text(filename);
  489. current_editor().set_focus(true);
  490. }
  491. bool make_is_available()
  492. {
  493. auto pid = fork();
  494. if (pid < 0)
  495. return false;
  496. if (!pid) {
  497. int rc = execlp("make", "make", "--version", nullptr);
  498. ASSERT(rc < 0);
  499. perror("execl");
  500. exit(127);
  501. }
  502. int wstatus;
  503. waitpid(pid, &wstatus, 0);
  504. return WEXITSTATUS(wstatus) == 0;
  505. }