main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  168. open_file(filename);
  169. });
  170. auto add_existing_file_action = GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  171. auto result = GUI::FilePicker::get_open_filepath("Add existing file to project");
  172. if (!result.has_value())
  173. return;
  174. auto& filename = result.value();
  175. if (!g_project->add_file(filename)) {
  176. GUI::MessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
  177. return;
  178. }
  179. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  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->toggle_index(g_project_tree_view->model()->index(0, 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 open_action = GUI::Action::create("Open Project", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  349. auto open_path = GUI::FilePicker::get_open_filepath();
  350. if (!open_path.has_value())
  351. return;
  352. open_project(open_path.value());
  353. open_file(g_project->default_file());
  354. update_actions();
  355. });
  356. auto save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  357. if (g_currently_open_file.is_empty())
  358. return;
  359. current_editor().write_to_file(g_currently_open_file);
  360. });
  361. toolbar.add_action(new_action);
  362. toolbar.add_action(add_existing_file_action);
  363. toolbar.add_action(save_action);
  364. toolbar.add_action(delete_action);
  365. toolbar.add_separator();
  366. toolbar.add_action(GUI::CommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  367. toolbar.add_action(GUI::CommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  368. toolbar.add_action(GUI::CommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  369. toolbar.add_separator();
  370. toolbar.add_action(GUI::CommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  371. toolbar.add_action(GUI::CommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  372. toolbar.add_separator();
  373. g_project_tree_view->on_activation = [&](auto& index) {
  374. auto filename = g_project_tree_view->model()->data(index, GUI::Model::Role::Custom).to_string();
  375. open_file(filename);
  376. };
  377. s_action_tab_widget = g_text_inner_splitter->add<GUI::TabWidget>();
  378. s_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  379. s_action_tab_widget->set_preferred_size(0, 24);
  380. auto reveal_action_tab = [&](auto& widget) {
  381. if (s_action_tab_widget->preferred_size().height() < 200)
  382. s_action_tab_widget->set_preferred_size(0, 200);
  383. s_action_tab_widget->set_active_widget(widget);
  384. };
  385. auto hide_action_tabs = [&] {
  386. s_action_tab_widget->set_preferred_size(0, 24);
  387. };
  388. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  389. hide_action_tabs();
  390. });
  391. auto add_editor_action = GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E }, [&](auto&) {
  392. add_new_editor(*g_text_inner_splitter);
  393. update_actions();
  394. });
  395. auto find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  396. auto terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Console");
  397. auto& locator = widget.add<Locator>();
  398. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  399. locator.open();
  400. });
  401. auto menubar = make<GUI::MenuBar>();
  402. auto app_menu = GUI::Menu::construct("HackStudio");
  403. app_menu->add_action(open_action);
  404. app_menu->add_action(save_action);
  405. app_menu->add_separator();
  406. app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  407. app.quit();
  408. }));
  409. menubar->add_menu(move(app_menu));
  410. auto project_menu = GUI::Menu::construct("Project");
  411. project_menu->add_action(new_action);
  412. project_menu->add_action(add_existing_file_action);
  413. menubar->add_menu(move(project_menu));
  414. auto edit_menu = GUI::Menu::construct("Edit");
  415. edit_menu->add_action(GUI::Action::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  416. reveal_action_tab(find_in_files_widget);
  417. find_in_files_widget->focus_textbox_and_select_all();
  418. }));
  419. menubar->add_menu(move(edit_menu));
  420. auto stop_action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/stop.png"), [&](auto&) {
  421. terminal_wrapper->kill_running_command();
  422. });
  423. stop_action->set_enabled(false);
  424. terminal_wrapper->on_command_exit = [&] {
  425. stop_action->set_enabled(false);
  426. };
  427. auto build_action = GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  428. reveal_action_tab(terminal_wrapper);
  429. build(terminal_wrapper);
  430. stop_action->set_enabled(true);
  431. });
  432. toolbar.add_action(build_action);
  433. auto run_action = GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/play.png"), [&](auto&) {
  434. reveal_action_tab(terminal_wrapper);
  435. run(terminal_wrapper);
  436. stop_action->set_enabled(true);
  437. });
  438. toolbar.add_action(run_action);
  439. toolbar.add_action(stop_action);
  440. auto build_menu = GUI::Menu::construct("Build");
  441. build_menu->add_action(build_action);
  442. build_menu->add_action(run_action);
  443. build_menu->add_action(stop_action);
  444. menubar->add_menu(move(build_menu));
  445. auto view_menu = GUI::Menu::construct("View");
  446. view_menu->add_action(hide_action_tabs_action);
  447. view_menu->add_action(open_locator_action);
  448. view_menu->add_separator();
  449. view_menu->add_action(add_editor_action);
  450. view_menu->add_action(remove_current_editor_action);
  451. menubar->add_menu(move(view_menu));
  452. auto small_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  453. auto help_menu = GUI::Menu::construct("Help");
  454. help_menu->add_action(GUI::Action::create("About", [&](auto&) {
  455. GUI::AboutDialog::show("HackStudio", small_icon, g_window);
  456. }));
  457. menubar->add_menu(move(help_menu));
  458. app.set_menubar(move(menubar));
  459. g_window->set_icon(small_icon);
  460. g_window->show();
  461. update_actions = [&]() {
  462. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  463. };
  464. g_open_file = open_file;
  465. open_file(g_project->default_file());
  466. update_actions();
  467. return app.exec();
  468. }
  469. void build(TerminalWrapper& wrapper)
  470. {
  471. if (g_project->type() == ProjectType::Javascript && g_currently_open_file.ends_with(".js"))
  472. wrapper.run_command(String::format("js -A %s", g_currently_open_file.characters()));
  473. else
  474. wrapper.run_command("make");
  475. }
  476. void run(TerminalWrapper& wrapper)
  477. {
  478. if (g_project->type() == ProjectType::Javascript && g_currently_open_file.ends_with(".js"))
  479. wrapper.run_command(String::format("js %s", g_currently_open_file.characters()));
  480. else
  481. wrapper.run_command("make run");
  482. }
  483. void open_project(String filename)
  484. {
  485. FileSystemPath path(filename);
  486. if (chdir(path.dirname().characters()) < 0) {
  487. perror("chdir");
  488. exit(1);
  489. }
  490. g_project = Project::load_from_file(filename);
  491. ASSERT(g_project);
  492. if (g_project_tree_view) {
  493. g_project_tree_view->set_model(g_project->model());
  494. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  495. g_project_tree_view->update();
  496. }
  497. }
  498. void open_file(const String& filename)
  499. {
  500. auto project_file = g_project->get_file(filename);
  501. if (project_file) {
  502. current_editor().set_document(const_cast<GUI::TextDocument&>(project_file->document()));
  503. current_editor().set_readonly(false);
  504. } else {
  505. auto external_file = ProjectFile::construct_with_name(filename);
  506. current_editor().set_document(const_cast<GUI::TextDocument&>(external_file->document()));
  507. current_editor().set_readonly(true);
  508. }
  509. if (filename.ends_with(".cpp") || filename.ends_with(".h"))
  510. current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
  511. else if (filename.ends_with(".js"))
  512. current_editor().set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
  513. else
  514. current_editor().set_syntax_highlighter(nullptr);
  515. if (filename.ends_with(".frm")) {
  516. set_edit_mode(EditMode::Form);
  517. } else {
  518. set_edit_mode(EditMode::Text);
  519. }
  520. g_currently_open_file = filename;
  521. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  522. g_project_tree_view->update();
  523. current_editor_wrapper().filename_label().set_text(filename);
  524. current_editor().set_focus(true);
  525. }
  526. bool make_is_available()
  527. {
  528. auto pid = fork();
  529. if (pid < 0)
  530. return false;
  531. if (!pid) {
  532. int rc = execlp("make", "make", "--version", nullptr);
  533. ASSERT(rc < 0);
  534. perror("execl");
  535. exit(127);
  536. }
  537. int wstatus;
  538. waitpid(pid, &wstatus, 0);
  539. return WEXITSTATUS(wstatus) == 0;
  540. }