main.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 "Debugger/DebugInfoWidget.h"
  28. #include "Debugger/Debugger.h"
  29. #include "Editor.h"
  30. #include "EditorWrapper.h"
  31. #include "FindInFilesWidget.h"
  32. #include "FormEditorWidget.h"
  33. #include "FormWidget.h"
  34. #include "Locator.h"
  35. #include "Project.h"
  36. #include "TerminalWrapper.h"
  37. #include "WidgetTool.h"
  38. #include "WidgetTreeModel.h"
  39. #include <AK/StringBuilder.h>
  40. #include <LibCore/Event.h>
  41. #include <LibCore/EventLoop.h>
  42. #include <LibCore/File.h>
  43. #include <LibDebug/DebugSession.h>
  44. #include <LibGUI/AboutDialog.h>
  45. #include <LibGUI/Action.h>
  46. #include <LibGUI/ActionGroup.h>
  47. #include <LibGUI/Application.h>
  48. #include <LibGUI/BoxLayout.h>
  49. #include <LibGUI/Button.h>
  50. #include <LibGUI/CppSyntaxHighlighter.h>
  51. #include <LibGUI/FilePicker.h>
  52. #include <LibGUI/INISyntaxHighlighter.h>
  53. #include <LibGUI/InputBox.h>
  54. #include <LibGUI/JSSyntaxHighlighter.h>
  55. #include <LibGUI/Label.h>
  56. #include <LibGUI/Menu.h>
  57. #include <LibGUI/MenuBar.h>
  58. #include <LibGUI/MessageBox.h>
  59. #include <LibGUI/Splitter.h>
  60. #include <LibGUI/StackWidget.h>
  61. #include <LibGUI/TabWidget.h>
  62. #include <LibGUI/TableView.h>
  63. #include <LibGUI/TextBox.h>
  64. #include <LibGUI/TextEditor.h>
  65. #include <LibGUI/ToolBar.h>
  66. #include <LibGUI/ToolBarContainer.h>
  67. #include <LibGUI/TreeView.h>
  68. #include <LibGUI/Widget.h>
  69. #include <LibGUI/Window.h>
  70. #include <LibThread/Lock.h>
  71. #include <LibThread/Thread.h>
  72. #include <LibVT/TerminalWidget.h>
  73. #include <spawn.h>
  74. #include <stdio.h>
  75. #include <sys/wait.h>
  76. #include <unistd.h>
  77. NonnullRefPtrVector<EditorWrapper> g_all_editor_wrappers;
  78. RefPtr<EditorWrapper> g_current_editor_wrapper;
  79. Function<void(String)> g_open_file;
  80. String g_currently_open_file;
  81. OwnPtr<Project> g_project;
  82. RefPtr<GUI::Window> g_window;
  83. RefPtr<GUI::TreeView> g_project_tree_view;
  84. RefPtr<GUI::StackWidget> g_right_hand_stack;
  85. RefPtr<GUI::Splitter> g_text_inner_splitter;
  86. RefPtr<GUI::Widget> g_form_inner_container;
  87. RefPtr<FormEditorWidget> g_form_editor_widget;
  88. static RefPtr<GUI::TabWidget> s_action_tab_widget;
  89. void add_new_editor(GUI::Widget& parent)
  90. {
  91. auto wrapper = EditorWrapper::construct(Debugger::on_breakpoint_change);
  92. if (s_action_tab_widget) {
  93. parent.insert_child_before(wrapper, *s_action_tab_widget);
  94. } else {
  95. parent.add_child(wrapper);
  96. }
  97. g_current_editor_wrapper = wrapper;
  98. g_all_editor_wrappers.append(wrapper);
  99. wrapper->editor().set_focus(true);
  100. }
  101. enum class EditMode {
  102. Text,
  103. Form,
  104. };
  105. void set_edit_mode(EditMode mode)
  106. {
  107. if (mode == EditMode::Text) {
  108. g_right_hand_stack->set_active_widget(g_text_inner_splitter);
  109. } else if (mode == EditMode::Form) {
  110. g_right_hand_stack->set_active_widget(g_form_inner_container);
  111. }
  112. }
  113. EditorWrapper& current_editor_wrapper()
  114. {
  115. ASSERT(g_current_editor_wrapper);
  116. return *g_current_editor_wrapper;
  117. }
  118. Editor& current_editor()
  119. {
  120. return current_editor_wrapper().editor();
  121. }
  122. NonnullRefPtr<EditorWrapper> get_editor_of_file(const String& file)
  123. {
  124. for (auto& wrapper : g_all_editor_wrappers) {
  125. String wrapper_file = wrapper.filename_label().text();
  126. if (wrapper_file == file || String::format("./%s", wrapper_file.characters()) == file) {
  127. return wrapper;
  128. }
  129. }
  130. ASSERT_NOT_REACHED();
  131. }
  132. String get_project_executable_path()
  133. {
  134. // e.g /my/project.hackstudio => /my/project
  135. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  136. return g_project->path().substring(0, g_project->path().index_of(".").value());
  137. }
  138. static void build(TerminalWrapper&);
  139. static void run(TerminalWrapper&);
  140. void open_project(String);
  141. void open_file(const String&);
  142. bool make_is_available();
  143. int main(int argc, char** argv)
  144. {
  145. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec unix fattr thread", nullptr) < 0) {
  146. perror("pledge");
  147. return 1;
  148. }
  149. auto app = GUI::Application::construct(argc, argv);
  150. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec fattr thread", nullptr) < 0) {
  151. perror("pledge");
  152. return 1;
  153. }
  154. Function<void()> update_actions;
  155. g_window = GUI::Window::construct();
  156. g_window->set_rect(90, 90, 840, 600);
  157. g_window->set_title("HackStudio");
  158. auto& widget = g_window->set_main_widget<GUI::Widget>();
  159. widget.set_fill_with_background_color(true);
  160. widget.set_layout<GUI::VerticalBoxLayout>();
  161. widget.layout()->set_spacing(2);
  162. StringBuilder path;
  163. path.append(getenv("PATH"));
  164. if (path.length())
  165. path.append(":");
  166. path.append("/bin:/usr/bin:/usr/local/bin");
  167. setenv("PATH", path.to_string().characters(), true);
  168. if (!make_is_available())
  169. GUI::MessageBox::show(g_window, "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);
  170. String argument_absolute_path;
  171. if (argc >= 2)
  172. argument_absolute_path = Core::File::real_path_for(argv[1]);
  173. if (!argument_absolute_path.is_empty() && argument_absolute_path.ends_with(".hackstudio"))
  174. open_project(argument_absolute_path);
  175. else
  176. open_project("/home/anon/Source/little/little.hackstudio");
  177. auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
  178. auto& toolbar = toolbar_container.add<GUI::ToolBar>();
  179. auto selected_file_names = [&] {
  180. Vector<String> files;
  181. g_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  182. files.append(g_project->model().data(index).as_string());
  183. });
  184. return files;
  185. };
  186. 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&) {
  187. String filename;
  188. if (GUI::InputBox::show(filename, g_window, "Enter name of new file:", "Add new file to project") != GUI::InputBox::ExecOK)
  189. return;
  190. auto file = Core::File::construct(filename);
  191. if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
  192. GUI::MessageBox::show(g_window, String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  193. return;
  194. }
  195. if (!g_project->add_file(filename)) {
  196. GUI::MessageBox::show(g_window, String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  197. // FIXME: Should we unlink the file here maybe?
  198. return;
  199. }
  200. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  201. open_file(filename);
  202. });
  203. auto add_existing_file_action = GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  204. auto result = GUI::FilePicker::get_open_filepath(g_window, "Add existing file to project");
  205. if (!result.has_value())
  206. return;
  207. auto& filename = result.value();
  208. if (!g_project->add_file(filename)) {
  209. GUI::MessageBox::show(g_window, String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  210. return;
  211. }
  212. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  213. open_file(filename);
  214. });
  215. auto delete_action = GUI::CommonActions::make_delete_action([&](const GUI::Action& action) {
  216. (void)action;
  217. auto files = selected_file_names();
  218. if (files.is_empty())
  219. return;
  220. String message;
  221. if (files.size() == 1) {
  222. message = String::format("Really remove %s from the project?", LexicalPath(files[0]).basename().characters());
  223. } else {
  224. message = String::format("Really remove %d files from the project?", files.size());
  225. }
  226. auto result = GUI::MessageBox::show(g_window,
  227. message,
  228. "Confirm deletion",
  229. GUI::MessageBox::Type::Warning,
  230. GUI::MessageBox::InputType::OKCancel);
  231. if (result == GUI::MessageBox::ExecCancel)
  232. return;
  233. for (auto& file : files) {
  234. if (!g_project->remove_file(file)) {
  235. GUI::MessageBox::show(g_window,
  236. String::format("Removing file %s from the project failed.", file.characters()),
  237. "Removal failed",
  238. GUI::MessageBox::Type::Error);
  239. break;
  240. }
  241. }
  242. });
  243. delete_action->set_enabled(false);
  244. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  245. project_tree_view_context_menu->add_action(new_action);
  246. project_tree_view_context_menu->add_action(add_existing_file_action);
  247. project_tree_view_context_menu->add_action(delete_action);
  248. auto& outer_splitter = widget.add<GUI::HorizontalSplitter>();
  249. g_project_tree_view = outer_splitter.add<GUI::TreeView>();
  250. g_project_tree_view->set_model(g_project->model());
  251. g_project_tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  252. g_project_tree_view->set_preferred_size(140, 0);
  253. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  254. g_project_tree_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  255. if (index.is_valid()) {
  256. project_tree_view_context_menu->popup(event.screen_position());
  257. }
  258. };
  259. g_project_tree_view->on_selection_change = [&] {
  260. delete_action->set_enabled(!g_project_tree_view->selection().is_empty());
  261. };
  262. g_right_hand_stack = outer_splitter.add<GUI::StackWidget>();
  263. g_form_inner_container = g_right_hand_stack->add<GUI::Widget>();
  264. g_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
  265. auto& form_widgets_toolbar = g_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
  266. form_widgets_toolbar.set_preferred_size(38, 0);
  267. GUI::ActionGroup tool_actions;
  268. tool_actions.set_exclusive(true);
  269. auto cursor_tool_action = GUI::Action::create_checkable("Cursor", Gfx::Bitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) {
  270. g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget));
  271. });
  272. cursor_tool_action->set_checked(true);
  273. tool_actions.add_action(cursor_tool_action);
  274. form_widgets_toolbar.add_action(cursor_tool_action);
  275. GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& reg) {
  276. auto icon_path = String::format("/res/icons/widgets/G%s.png", reg.class_name().characters());
  277. auto action = GUI::Action::create_checkable(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg](auto&) {
  278. g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg));
  279. auto widget = reg.construct();
  280. g_form_editor_widget->form_widget().add_child(widget);
  281. widget->set_relative_rect(30, 30, 30, 30);
  282. g_form_editor_widget->model().update();
  283. });
  284. action->set_checked(false);
  285. tool_actions.add_action(action);
  286. form_widgets_toolbar.add_action(move(action));
  287. });
  288. auto& form_editor_inner_splitter = g_form_inner_container->add<GUI::HorizontalSplitter>();
  289. g_form_editor_widget = form_editor_inner_splitter.add<FormEditorWidget>();
  290. auto& form_editing_pane_container = form_editor_inner_splitter.add<GUI::VerticalSplitter>();
  291. form_editing_pane_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  292. form_editing_pane_container.set_preferred_size(190, 0);
  293. form_editing_pane_container.set_layout<GUI::VerticalBoxLayout>();
  294. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  295. auto& wrapper = form_editing_pane_container.add<GUI::Widget>();
  296. wrapper.set_layout<GUI::VerticalBoxLayout>();
  297. auto& label = wrapper.add<GUI::Label>(text);
  298. label.set_fill_with_background_color(true);
  299. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  300. label.set_font(Gfx::Font::default_bold_font());
  301. label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  302. label.set_preferred_size(0, 16);
  303. wrapper.add_child(pane_widget);
  304. };
  305. auto form_widget_tree_view = GUI::TreeView::construct();
  306. form_widget_tree_view->set_model(g_form_editor_widget->model());
  307. form_widget_tree_view->on_selection_change = [&] {
  308. g_form_editor_widget->selection().disable_hooks();
  309. g_form_editor_widget->selection().clear();
  310. form_widget_tree_view->selection().for_each_index([&](auto& index) {
  311. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  312. // since that would allow you to drag-move the FormWidget.
  313. if (index.internal_data() != &g_form_editor_widget->form_widget())
  314. g_form_editor_widget->selection().add(*(GUI::Widget*)index.internal_data());
  315. });
  316. g_form_editor_widget->update();
  317. g_form_editor_widget->selection().enable_hooks();
  318. };
  319. g_form_editor_widget->selection().on_add = [&](auto& widget) {
  320. form_widget_tree_view->selection().add(g_form_editor_widget->model().index_for_widget(widget));
  321. };
  322. g_form_editor_widget->selection().on_remove = [&](auto& widget) {
  323. form_widget_tree_view->selection().remove(g_form_editor_widget->model().index_for_widget(widget));
  324. };
  325. g_form_editor_widget->selection().on_clear = [&] {
  326. form_widget_tree_view->selection().clear();
  327. };
  328. add_properties_pane("Form widget tree:", form_widget_tree_view);
  329. add_properties_pane("Widget properties:", GUI::TableView::construct());
  330. g_text_inner_splitter = g_right_hand_stack->add<GUI::VerticalSplitter>();
  331. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  332. add_new_editor(*g_text_inner_splitter);
  333. auto switch_to_next_editor = GUI::Action::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  334. if (g_all_editor_wrappers.size() <= 1)
  335. return;
  336. Vector<EditorWrapper*> wrappers;
  337. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  338. wrappers.append(&child);
  339. return IterationDecision::Continue;
  340. });
  341. for (size_t i = 0; i < wrappers.size(); ++i) {
  342. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  343. if (i == wrappers.size() - 1)
  344. wrappers[0]->editor().set_focus(true);
  345. else
  346. wrappers[i + 1]->editor().set_focus(true);
  347. }
  348. }
  349. });
  350. auto switch_to_previous_editor = GUI::Action::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  351. if (g_all_editor_wrappers.size() <= 1)
  352. return;
  353. Vector<EditorWrapper*> wrappers;
  354. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  355. wrappers.append(&child);
  356. return IterationDecision::Continue;
  357. });
  358. for (int i = wrappers.size() - 1; i >= 0; --i) {
  359. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  360. if (i == 0)
  361. wrappers.last()->editor().set_focus(true);
  362. else
  363. wrappers[i - 1]->editor().set_focus(true);
  364. }
  365. }
  366. });
  367. auto remove_current_editor_action = GUI::Action::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  368. if (g_all_editor_wrappers.size() <= 1)
  369. return;
  370. auto wrapper = g_current_editor_wrapper;
  371. switch_to_next_editor->activate();
  372. g_text_inner_splitter->remove_child(*wrapper);
  373. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  374. update_actions();
  375. });
  376. 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&) {
  377. auto open_path = GUI::FilePicker::get_open_filepath(g_window, "Open project");
  378. if (!open_path.has_value())
  379. return;
  380. open_project(open_path.value());
  381. open_file(g_project->default_file());
  382. update_actions();
  383. });
  384. auto save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  385. if (g_currently_open_file.is_empty())
  386. return;
  387. current_editor().write_to_file(g_currently_open_file);
  388. });
  389. toolbar.add_action(new_action);
  390. toolbar.add_action(add_existing_file_action);
  391. toolbar.add_action(save_action);
  392. toolbar.add_action(delete_action);
  393. toolbar.add_separator();
  394. toolbar.add_action(GUI::CommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  395. toolbar.add_action(GUI::CommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  396. toolbar.add_action(GUI::CommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  397. toolbar.add_separator();
  398. toolbar.add_action(GUI::CommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  399. toolbar.add_action(GUI::CommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  400. toolbar.add_separator();
  401. g_project_tree_view->on_activation = [&](auto& index) {
  402. auto filename = g_project_tree_view->model()->data(index, GUI::Model::Role::Custom).to_string();
  403. open_file(filename);
  404. };
  405. s_action_tab_widget = g_text_inner_splitter->add<GUI::TabWidget>();
  406. s_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  407. s_action_tab_widget->set_preferred_size(0, 24);
  408. s_action_tab_widget->on_change = [&](auto&) { update_actions(); };
  409. auto reveal_action_tab = [&](auto& widget) {
  410. if (s_action_tab_widget->preferred_size().height() < 200)
  411. s_action_tab_widget->set_preferred_size(0, 200);
  412. s_action_tab_widget->set_active_widget(&widget);
  413. };
  414. auto hide_action_tabs = [&] {
  415. s_action_tab_widget->set_preferred_size(0, 24);
  416. };
  417. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  418. hide_action_tabs();
  419. });
  420. auto add_editor_action = GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E },
  421. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-text-editor.png"),
  422. [&](auto&) {
  423. add_new_editor(*g_text_inner_splitter);
  424. update_actions();
  425. });
  426. auto add_terminal_action = GUI::Action::create("Add new Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  427. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"),
  428. [&](auto&) {
  429. auto& terminal = s_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  430. reveal_action_tab(terminal);
  431. update_actions();
  432. terminal.terminal()->set_focus(true);
  433. });
  434. auto remove_current_terminal_action = GUI::Action::create("Remove current Terminal", { Mod_Alt | Mod_Shift, Key_T }, [&](auto&) {
  435. auto widget = s_action_tab_widget->active_widget();
  436. if (!widget)
  437. return;
  438. if (strcmp(widget->class_name(), "TerminalWrapper") != 0)
  439. return;
  440. auto terminal = reinterpret_cast<TerminalWrapper*>(widget);
  441. if (!terminal->user_spawned())
  442. return;
  443. s_action_tab_widget->remove_tab(*terminal);
  444. update_actions();
  445. });
  446. auto& find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  447. auto& terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Build", false);
  448. auto& debug_info_widget = s_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  449. auto& locator = widget.add<Locator>();
  450. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  451. locator.open();
  452. });
  453. auto menubar = GUI::MenuBar::construct();
  454. auto& app_menu = menubar->add_menu("HackStudio");
  455. app_menu.add_action(open_action);
  456. app_menu.add_action(save_action);
  457. app_menu.add_separator();
  458. app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  459. app->quit();
  460. }));
  461. auto& project_menu = menubar->add_menu("Project");
  462. project_menu.add_action(new_action);
  463. project_menu.add_action(add_existing_file_action);
  464. auto& edit_menu = menubar->add_menu("Edit");
  465. edit_menu.add_action(GUI::Action::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"), [&](auto&) {
  466. reveal_action_tab(find_in_files_widget);
  467. find_in_files_widget.focus_textbox_and_select_all();
  468. }));
  469. auto stop_action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/program-stop.png"), [&](auto&) {
  470. terminal_wrapper.kill_running_command();
  471. });
  472. stop_action->set_enabled(false);
  473. terminal_wrapper.on_command_exit = [&] {
  474. stop_action->set_enabled(false);
  475. };
  476. auto build_action = GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  477. reveal_action_tab(terminal_wrapper);
  478. build(terminal_wrapper);
  479. stop_action->set_enabled(true);
  480. });
  481. toolbar.add_action(build_action);
  482. toolbar.add_separator();
  483. auto run_action = GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/program-run.png"), [&](auto&) {
  484. reveal_action_tab(terminal_wrapper);
  485. run(terminal_wrapper);
  486. stop_action->set_enabled(true);
  487. });
  488. RefPtr<LibThread::Thread> debugger_thread;
  489. auto debug_action = GUI::Action::create("Debug", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-run.png"), [&](auto&) {
  490. if (g_project->type() != ProjectType::Cpp) {
  491. GUI::MessageBox::show(g_window, String::format("Cannot debug current project type", get_project_executable_path().characters()), "Error", GUI::MessageBox::Type::Error);
  492. return;
  493. }
  494. if (!GUI::FilePicker::file_exists(get_project_executable_path())) {
  495. GUI::MessageBox::show(g_window, String::format("Could not find file: %s. (did you build the project?)", get_project_executable_path().characters()), "Error", GUI::MessageBox::Type::Error);
  496. return;
  497. }
  498. if (Debugger::the().session()) {
  499. GUI::MessageBox::show(g_window, "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  500. return;
  501. }
  502. Debugger::the().set_executable_path(get_project_executable_path());
  503. debugger_thread = adopt(*new LibThread::Thread(Debugger::start_static));
  504. debugger_thread->start();
  505. });
  506. auto continue_action = GUI::Action::create("Continue", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-continue.png"), [&](auto&) {
  507. pthread_mutex_lock(Debugger::the().continue_mutex());
  508. Debugger::the().set_continue_type(Debugger::ContinueType::Continue);
  509. pthread_cond_signal(Debugger::the().continue_cond());
  510. pthread_mutex_unlock(Debugger::the().continue_mutex());
  511. });
  512. auto single_step_action = GUI::Action::create("Single Step", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-single-step.png"), [&](auto&) {
  513. pthread_mutex_lock(Debugger::the().continue_mutex());
  514. Debugger::the().set_continue_type(Debugger::ContinueType::SourceSingleStep);
  515. pthread_cond_signal(Debugger::the().continue_cond());
  516. pthread_mutex_unlock(Debugger::the().continue_mutex());
  517. });
  518. continue_action->set_enabled(false);
  519. single_step_action->set_enabled(false);
  520. toolbar.add_action(run_action);
  521. toolbar.add_action(stop_action);
  522. toolbar.add_separator();
  523. toolbar.add_action(debug_action);
  524. toolbar.add_action(continue_action);
  525. toolbar.add_action(single_step_action);
  526. RefPtr<EditorWrapper> current_editor_in_execution;
  527. Debugger::initialize(
  528. [&](const PtraceRegisters& regs) {
  529. dbg() << "Program stopped";
  530. ASSERT(Debugger::the().session());
  531. const auto& debug_session = *Debugger::the().session();
  532. auto source_position = debug_session.debug_info().get_source_position(regs.eip);
  533. if (!source_position.has_value()) {
  534. dbg() << "Could not find source position for address: " << (void*)regs.eip;
  535. return Debugger::HasControlPassedToUser::No;
  536. }
  537. current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  538. current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  539. debug_info_widget.update_state(debug_session, regs);
  540. continue_action->set_enabled(true);
  541. single_step_action->set_enabled(true);
  542. reveal_action_tab(debug_info_widget);
  543. return Debugger::HasControlPassedToUser::Yes;
  544. },
  545. [&]() {
  546. dbg() << "Program continued";
  547. continue_action->set_enabled(false);
  548. single_step_action->set_enabled(false);
  549. if (current_editor_in_execution) {
  550. current_editor_in_execution->editor().clear_execution_position();
  551. }
  552. },
  553. [&]() {
  554. dbg() << "Program exited";
  555. debug_info_widget.program_stopped();
  556. hide_action_tabs();
  557. Core::EventLoop::main().post_event(*g_window, make<Core::DeferredInvocationEvent>([=](auto&) {
  558. GUI::MessageBox::show(g_window, "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  559. }));
  560. Core::EventLoop::wake();
  561. });
  562. auto& build_menu = menubar->add_menu("Build");
  563. build_menu.add_action(build_action);
  564. build_menu.add_separator();
  565. build_menu.add_action(run_action);
  566. build_menu.add_action(stop_action);
  567. build_menu.add_separator();
  568. build_menu.add_action(debug_action);
  569. auto& view_menu = menubar->add_menu("View");
  570. view_menu.add_action(hide_action_tabs_action);
  571. view_menu.add_action(open_locator_action);
  572. view_menu.add_separator();
  573. view_menu.add_action(add_editor_action);
  574. view_menu.add_action(remove_current_editor_action);
  575. view_menu.add_action(add_terminal_action);
  576. view_menu.add_action(remove_current_terminal_action);
  577. auto& help_menu = menubar->add_menu("Help");
  578. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  579. GUI::AboutDialog::show("HackStudio", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"), g_window);
  580. }));
  581. app->set_menubar(move(menubar));
  582. g_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  583. g_window->show();
  584. update_actions = [&]() {
  585. auto is_remove_terminal_enabled = []() {
  586. auto widget = s_action_tab_widget->active_widget();
  587. if (!widget)
  588. return false;
  589. if (strcmp(widget->class_name(), "TerminalWrapper") != 0)
  590. return false;
  591. if (!reinterpret_cast<TerminalWrapper*>(widget)->user_spawned())
  592. return false;
  593. return true;
  594. };
  595. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  596. remove_current_terminal_action->set_enabled(is_remove_terminal_enabled());
  597. };
  598. g_open_file = open_file;
  599. if (!argument_absolute_path.is_empty() && !argument_absolute_path.ends_with(".hackstudio"))
  600. open_file(argument_absolute_path);
  601. else
  602. open_file(g_project->default_file());
  603. update_actions();
  604. return app->exec();
  605. }
  606. void build(TerminalWrapper& wrapper)
  607. {
  608. if (g_project->type() == ProjectType::JavaScript && g_currently_open_file.ends_with(".js"))
  609. wrapper.run_command(String::format("js -A %s", g_currently_open_file.characters()));
  610. else
  611. wrapper.run_command("make");
  612. }
  613. void run(TerminalWrapper& wrapper)
  614. {
  615. if (g_project->type() == ProjectType::JavaScript && g_currently_open_file.ends_with(".js"))
  616. wrapper.run_command(String::format("js %s", g_currently_open_file.characters()));
  617. else
  618. wrapper.run_command("make run");
  619. }
  620. void open_project(String filename)
  621. {
  622. LexicalPath lexical_path(filename);
  623. if (chdir(lexical_path.dirname().characters()) < 0) {
  624. perror("chdir");
  625. exit(1);
  626. }
  627. g_project = Project::load_from_file(filename);
  628. ASSERT(g_project);
  629. if (g_project_tree_view) {
  630. g_project_tree_view->set_model(g_project->model());
  631. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  632. g_project_tree_view->update();
  633. }
  634. if (Debugger::is_initialized()) {
  635. Debugger::the().reset_breakpoints();
  636. }
  637. }
  638. void open_file(const String& filename)
  639. {
  640. auto project_file = g_project->get_file(filename);
  641. if (project_file) {
  642. current_editor().set_document(const_cast<GUI::TextDocument&>(project_file->document()));
  643. current_editor().set_mode(GUI::TextEditor::Editable);
  644. } else {
  645. auto external_file = ProjectFile::construct_with_name(filename);
  646. current_editor().set_document(const_cast<GUI::TextDocument&>(external_file->document()));
  647. current_editor().set_mode(GUI::TextEditor::ReadOnly);
  648. }
  649. if (filename.ends_with(".cpp") || filename.ends_with(".h"))
  650. current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
  651. else if (filename.ends_with(".js"))
  652. current_editor().set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
  653. else if (filename.ends_with(".ini"))
  654. current_editor().set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  655. else
  656. current_editor().set_syntax_highlighter(nullptr);
  657. if (filename.ends_with(".frm")) {
  658. set_edit_mode(EditMode::Form);
  659. } else {
  660. set_edit_mode(EditMode::Text);
  661. }
  662. g_currently_open_file = filename;
  663. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  664. g_project_tree_view->update();
  665. current_editor_wrapper().filename_label().set_text(filename);
  666. current_editor().set_focus(true);
  667. }
  668. bool make_is_available()
  669. {
  670. pid_t pid;
  671. const char* argv[] = { "make", "--version", nullptr };
  672. if ((errno = posix_spawnp(&pid, "make", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  673. perror("posix_spawn");
  674. return false;
  675. }
  676. int wstatus;
  677. waitpid(pid, &wstatus, 0);
  678. return WEXITSTATUS(wstatus) == 0;
  679. }