HackStudioWidget.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  4. * Copyright (c) 2020, the SerenityOS developers
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "HackStudioWidget.h"
  29. #include "CursorTool.h"
  30. #include "Debugger/DebugInfoWidget.h"
  31. #include "Debugger/Debugger.h"
  32. #include "Debugger/DisassemblyWidget.h"
  33. #include "Editor.h"
  34. #include "EditorWrapper.h"
  35. #include "FindInFilesWidget.h"
  36. #include "FormEditorWidget.h"
  37. #include "FormWidget.h"
  38. #include "Git/DiffViewer.h"
  39. #include "Git/GitWidget.h"
  40. #include "HackStudio.h"
  41. #include "HackStudioWidget.h"
  42. #include "Locator.h"
  43. #include "Project.h"
  44. #include "TerminalWrapper.h"
  45. #include "WidgetTool.h"
  46. #include "WidgetTreeModel.h"
  47. #include <AK/StringBuilder.h>
  48. #include <LibCore/ArgsParser.h>
  49. #include <LibCore/Event.h>
  50. #include <LibCore/EventLoop.h>
  51. #include <LibCore/File.h>
  52. #include <LibDebug/DebugSession.h>
  53. #include <LibGUI/AboutDialog.h>
  54. #include <LibGUI/Action.h>
  55. #include <LibGUI/ActionGroup.h>
  56. #include <LibGUI/Application.h>
  57. #include <LibGUI/BoxLayout.h>
  58. #include <LibGUI/Button.h>
  59. #include <LibGUI/CppSyntaxHighlighter.h>
  60. #include <LibGUI/FilePicker.h>
  61. #include <LibGUI/INISyntaxHighlighter.h>
  62. #include <LibGUI/InputBox.h>
  63. #include <LibGUI/JSSyntaxHighlighter.h>
  64. #include <LibGUI/Label.h>
  65. #include <LibGUI/Menu.h>
  66. #include <LibGUI/MenuBar.h>
  67. #include <LibGUI/MessageBox.h>
  68. #include <LibGUI/Splitter.h>
  69. #include <LibGUI/StackWidget.h>
  70. #include <LibGUI/TabWidget.h>
  71. #include <LibGUI/TableView.h>
  72. #include <LibGUI/TextBox.h>
  73. #include <LibGUI/TextEditor.h>
  74. #include <LibGUI/ToolBar.h>
  75. #include <LibGUI/ToolBarContainer.h>
  76. #include <LibGUI/TreeView.h>
  77. #include <LibGUI/Widget.h>
  78. #include <LibGUI/Window.h>
  79. #include <LibThread/Lock.h>
  80. #include <LibThread/Thread.h>
  81. #include <LibVT/TerminalWidget.h>
  82. #include <fcntl.h>
  83. #include <spawn.h>
  84. #include <stdio.h>
  85. #include <sys/types.h>
  86. #include <sys/wait.h>
  87. #include <unistd.h>
  88. namespace HackStudio {
  89. HackStudioWidget::HackStudioWidget(const String& path_to_project)
  90. {
  91. set_fill_with_background_color(true);
  92. set_layout<GUI::VerticalBoxLayout>();
  93. layout()->set_spacing(2);
  94. open_project(path_to_project);
  95. auto& toolbar_container = add<GUI::ToolBarContainer>();
  96. auto& outer_splitter = add<GUI::HorizontalSplitter>();
  97. create_project_tree_view(outer_splitter);
  98. m_project_tree_view_context_menu = create_project_tree_view_context_menu();
  99. m_right_hand_splitter = outer_splitter.add<GUI::VerticalSplitter>();
  100. m_right_hand_stack = m_right_hand_splitter->add<GUI::StackWidget>();
  101. create_form_editor(*m_right_hand_stack);
  102. m_diff_viewer = m_right_hand_stack->add<DiffViewer>();
  103. m_editors_splitter = m_right_hand_stack->add<GUI::VerticalSplitter>();
  104. m_editors_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  105. add_new_editor(*m_editors_splitter);
  106. m_switch_to_next_editor = create_switch_to_next_editor_action();
  107. m_switch_to_previous_editor = create_switch_to_previous_editor_action();
  108. m_remove_current_editor_action = create_remove_current_editor_action();
  109. m_open_action = create_open_action();
  110. m_save_action = create_save_action();
  111. create_action_tab(*m_right_hand_splitter);
  112. m_add_editor_action = create_add_editor_action();
  113. m_add_terminal_action = create_add_terminal_action();
  114. m_remove_current_terminal_action = create_remove_current_terminal_action();
  115. m_locator = add<Locator>();
  116. m_terminal_wrapper->on_command_exit = [this] {
  117. m_stop_action->set_enabled(false);
  118. };
  119. m_build_action = create_build_action();
  120. m_run_action = create_run_action();
  121. m_stop_action = create_stop_action();
  122. m_debug_action = create_debug_action();
  123. initialize_debugger();
  124. create_toolbar(toolbar_container);
  125. }
  126. void HackStudioWidget::update_actions()
  127. {
  128. auto is_remove_terminal_enabled = [this]() {
  129. auto widget = m_action_tab_widget->active_widget();
  130. if (!widget)
  131. return false;
  132. if (StringView { "TerminalWrapper" } != widget->class_name())
  133. return false;
  134. if (!reinterpret_cast<TerminalWrapper*>(widget)->user_spawned())
  135. return false;
  136. return true;
  137. };
  138. m_remove_current_editor_action->set_enabled(m_all_editor_wrappers.size() > 1);
  139. m_remove_current_terminal_action->set_enabled(is_remove_terminal_enabled());
  140. }
  141. void HackStudioWidget::on_action_tab_change()
  142. {
  143. update_actions();
  144. auto git_widget = m_action_tab_widget->active_widget();
  145. if (!git_widget)
  146. return;
  147. if (StringView { "GitWidget" } != git_widget->class_name())
  148. return;
  149. reinterpret_cast<GitWidget*>(git_widget)->refresh();
  150. }
  151. void HackStudioWidget::open_project(String filename)
  152. {
  153. LexicalPath lexical_path(filename);
  154. if (chdir(lexical_path.dirname().characters()) < 0) {
  155. perror("chdir");
  156. exit(1);
  157. }
  158. m_project = Project::load_from_file(filename);
  159. ASSERT(m_project);
  160. if (m_project_tree_view) {
  161. m_project_tree_view->set_model(m_project->model());
  162. m_project_tree_view->toggle_index(m_project_tree_view->model()->index(0, 0));
  163. m_project_tree_view->update();
  164. }
  165. if (Debugger::is_initialized()) {
  166. Debugger::the().reset_breakpoints();
  167. }
  168. }
  169. Vector<String> HackStudioWidget::selected_file_names() const
  170. {
  171. Vector<String> files;
  172. m_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  173. files.append(index.data().as_string());
  174. });
  175. return files;
  176. }
  177. void HackStudioWidget::open_file(const String& filename)
  178. {
  179. auto project_file = m_project->get_file(filename);
  180. if (project_file) {
  181. current_editor().set_document(const_cast<GUI::TextDocument&>(project_file->document()));
  182. current_editor().set_mode(GUI::TextEditor::Editable);
  183. } else {
  184. auto external_file = ProjectFile::construct_with_name(filename);
  185. current_editor().set_document(const_cast<GUI::TextDocument&>(external_file->document()));
  186. current_editor().set_mode(GUI::TextEditor::ReadOnly);
  187. }
  188. if (filename.ends_with(".cpp") || filename.ends_with(".h"))
  189. current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
  190. else if (filename.ends_with(".js"))
  191. current_editor().set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
  192. else if (filename.ends_with(".ini"))
  193. current_editor().set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  194. else
  195. current_editor().set_syntax_highlighter(nullptr);
  196. if (filename.ends_with(".frm")) {
  197. set_edit_mode(EditMode::Form);
  198. } else {
  199. set_edit_mode(EditMode::Text);
  200. }
  201. m_currently_open_file = filename;
  202. window()->set_title(String::format("%s - HackStudio", m_currently_open_file.characters()));
  203. m_project_tree_view->update();
  204. current_editor_wrapper().filename_label().set_text(filename);
  205. current_editor().set_focus(true);
  206. }
  207. EditorWrapper& HackStudioWidget::current_editor_wrapper()
  208. {
  209. ASSERT(m_current_editor_wrapper);
  210. return *m_current_editor_wrapper;
  211. }
  212. GUI::TextEditor& HackStudioWidget::current_editor()
  213. {
  214. return current_editor_wrapper().editor();
  215. }
  216. void HackStudioWidget::set_edit_mode(EditMode mode)
  217. {
  218. if (mode == EditMode::Text) {
  219. m_right_hand_stack->set_active_widget(m_editors_splitter);
  220. } else if (mode == EditMode::Form) {
  221. m_right_hand_stack->set_active_widget(m_form_inner_container);
  222. } else if (mode == EditMode::Diff) {
  223. m_right_hand_stack->set_active_widget(m_diff_viewer);
  224. } else {
  225. ASSERT_NOT_REACHED();
  226. }
  227. m_right_hand_stack->active_widget()->update();
  228. }
  229. NonnullRefPtr<GUI::Menu> HackStudioWidget::create_project_tree_view_context_menu()
  230. {
  231. m_open_selected_action = create_open_selected_action();
  232. m_new_action = create_new_action();
  233. m_add_existing_file_action = create_add_existing_file_action();
  234. m_delete_action = create_delete_action();
  235. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  236. project_tree_view_context_menu->add_action(*m_open_selected_action);
  237. // TODO: Rename, cut, copy, duplicate with new name, show containing folder ...
  238. project_tree_view_context_menu->add_separator();
  239. project_tree_view_context_menu->add_action(*m_new_action);
  240. project_tree_view_context_menu->add_action(*m_add_existing_file_action);
  241. project_tree_view_context_menu->add_action(*m_delete_action);
  242. return project_tree_view_context_menu;
  243. }
  244. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_action()
  245. {
  246. return GUI::Action::create("Add new file to project...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) {
  247. String filename;
  248. if (GUI::InputBox::show(filename, window(), "Enter name of new file:", "Add new file to project") != GUI::InputBox::ExecOK)
  249. return;
  250. auto file = Core::File::construct(filename);
  251. if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
  252. GUI::MessageBox::show(window(), String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  253. return;
  254. }
  255. if (!m_project->add_file(filename)) {
  256. GUI::MessageBox::show(window(), String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  257. // FIXME: Should we unlink the file here maybe?
  258. return;
  259. }
  260. m_project_tree_view->toggle_index(m_project_tree_view->model()->index(0, 0));
  261. open_file(filename);
  262. });
  263. }
  264. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_selected_action()
  265. {
  266. auto open_selected_action = GUI::Action::create("Open", [this](const GUI::Action&) {
  267. auto files = selected_file_names();
  268. for (auto& file : files)
  269. open_file(file);
  270. });
  271. open_selected_action->set_enabled(true);
  272. return open_selected_action;
  273. }
  274. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_existing_file_action()
  275. {
  276. return GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [this](auto&) {
  277. auto result = GUI::FilePicker::get_open_filepath(window(), "Add existing file to project");
  278. if (!result.has_value())
  279. return;
  280. auto& filename = result.value();
  281. if (!m_project->add_file(filename)) {
  282. GUI::MessageBox::show(window(), String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  283. return;
  284. }
  285. m_project_tree_view->toggle_index(m_project_tree_view->model()->index(0, 0));
  286. open_file(filename);
  287. });
  288. }
  289. NonnullRefPtr<GUI::Action> HackStudioWidget::create_delete_action()
  290. {
  291. auto delete_action = GUI::CommonActions::make_delete_action([this](const GUI::Action&) {
  292. auto files = selected_file_names();
  293. if (files.is_empty())
  294. return;
  295. String message;
  296. if (files.size() == 1) {
  297. message = String::format("Really remove %s from the project?", LexicalPath(files[0]).basename().characters());
  298. } else {
  299. message = String::format("Really remove %d files from the project?", files.size());
  300. }
  301. auto result = GUI::MessageBox::show(window(),
  302. message,
  303. "Confirm deletion",
  304. GUI::MessageBox::Type::Warning,
  305. GUI::MessageBox::InputType::OKCancel);
  306. if (result == GUI::MessageBox::ExecCancel)
  307. return;
  308. for (auto& file : files) {
  309. if (!m_project->remove_file(file)) {
  310. GUI::MessageBox::show(window(),
  311. String::format("Removing file %s from the project failed.", file.characters()),
  312. "Removal failed",
  313. GUI::MessageBox::Type::Error);
  314. break;
  315. }
  316. }
  317. });
  318. delete_action->set_enabled(false);
  319. return delete_action;
  320. }
  321. void HackStudioWidget::add_new_editor(GUI::Widget& parent)
  322. {
  323. auto wrapper = EditorWrapper::construct(Debugger::on_breakpoint_change);
  324. if (m_action_tab_widget) {
  325. parent.insert_child_before(wrapper, *m_action_tab_widget);
  326. } else {
  327. parent.add_child(wrapper);
  328. }
  329. m_current_editor_wrapper = wrapper;
  330. m_all_editor_wrappers.append(wrapper);
  331. wrapper->editor().set_focus(true);
  332. }
  333. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_next_editor_action()
  334. {
  335. return GUI::Action::create("Switch to next editor", { Mod_Ctrl, Key_E }, [this](auto&) {
  336. if (m_all_editor_wrappers.size() <= 1)
  337. return;
  338. Vector<EditorWrapper*> wrappers;
  339. m_editors_splitter->for_each_child_of_type<EditorWrapper>([this, &wrappers](auto& child) {
  340. wrappers.append(&child);
  341. return IterationDecision::Continue;
  342. });
  343. for (size_t i = 0; i < wrappers.size(); ++i) {
  344. if (m_current_editor_wrapper.ptr() == wrappers[i]) {
  345. if (i == wrappers.size() - 1)
  346. wrappers[0]->editor().set_focus(true);
  347. else
  348. wrappers[i + 1]->editor().set_focus(true);
  349. }
  350. }
  351. });
  352. }
  353. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_previous_editor_action()
  354. {
  355. return GUI::Action::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [this](auto&) {
  356. if (m_all_editor_wrappers.size() <= 1)
  357. return;
  358. Vector<EditorWrapper*> wrappers;
  359. m_editors_splitter->for_each_child_of_type<EditorWrapper>([this, &wrappers](auto& child) {
  360. wrappers.append(&child);
  361. return IterationDecision::Continue;
  362. });
  363. for (int i = wrappers.size() - 1; i >= 0; --i) {
  364. if (m_current_editor_wrapper.ptr() == wrappers[i]) {
  365. if (i == 0)
  366. wrappers.last()->editor().set_focus(true);
  367. else
  368. wrappers[i - 1]->editor().set_focus(true);
  369. }
  370. }
  371. });
  372. }
  373. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_editor_action()
  374. {
  375. return GUI::Action::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [this](auto&) {
  376. if (m_all_editor_wrappers.size() <= 1)
  377. return;
  378. auto wrapper = m_current_editor_wrapper;
  379. m_switch_to_next_editor->activate();
  380. m_editors_splitter->remove_child(*wrapper);
  381. m_all_editor_wrappers.remove_first_matching([&wrapper](auto& entry) { return entry == wrapper.ptr(); });
  382. update_actions();
  383. });
  384. }
  385. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_action()
  386. {
  387. return GUI::Action::create("Open project...", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [this](auto&) {
  388. auto open_path = GUI::FilePicker::get_open_filepath(window(), "Open project");
  389. if (!open_path.has_value())
  390. return;
  391. open_project(open_path.value());
  392. open_file(m_project->default_file());
  393. update_actions();
  394. });
  395. }
  396. NonnullRefPtr<GUI::Action> HackStudioWidget::create_save_action()
  397. {
  398. return GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [this](auto&) {
  399. if (m_currently_open_file.is_empty())
  400. return;
  401. current_editor().write_to_file(m_currently_open_file);
  402. if (m_git_widget->initialized())
  403. m_git_widget->refresh();
  404. });
  405. }
  406. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_terminal_action()
  407. {
  408. return GUI::Action::create("Remove current Terminal", { Mod_Alt | Mod_Shift, Key_T }, [this](auto&) {
  409. auto widget = m_action_tab_widget->active_widget();
  410. if (!widget)
  411. return;
  412. if (strcmp(widget->class_name(), "TerminalWrapper") != 0)
  413. return;
  414. auto terminal = reinterpret_cast<TerminalWrapper*>(widget);
  415. if (!terminal->user_spawned())
  416. return;
  417. m_action_tab_widget->remove_tab(*terminal);
  418. update_actions();
  419. });
  420. }
  421. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_editor_action()
  422. {
  423. return GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E },
  424. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-text-editor.png"),
  425. [this](auto&) {
  426. add_new_editor(*m_editors_splitter);
  427. update_actions();
  428. });
  429. }
  430. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_terminal_action()
  431. {
  432. return GUI::Action::create("Add new Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  433. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"),
  434. [this](auto&) {
  435. auto& terminal = m_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  436. reveal_action_tab(terminal);
  437. update_actions();
  438. terminal.terminal()->set_focus(true);
  439. });
  440. }
  441. void HackStudioWidget::reveal_action_tab(GUI::Widget& widget)
  442. {
  443. if (m_action_tab_widget->preferred_size().height() < 200)
  444. m_action_tab_widget->set_preferred_size(0, 200);
  445. m_action_tab_widget->set_active_widget(&widget);
  446. }
  447. NonnullRefPtr<GUI::Action> HackStudioWidget::create_debug_action()
  448. {
  449. return GUI::Action::create("Debug", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-run.png"), [this](auto&) {
  450. if (m_project->type() != ProjectType::Cpp) {
  451. GUI::MessageBox::show(window(), String::format("Cannot debug current project type", get_project_executable_path().characters()), "Error", GUI::MessageBox::Type::Error);
  452. return;
  453. }
  454. if (!GUI::FilePicker::file_exists(get_project_executable_path())) {
  455. GUI::MessageBox::show(window(), String::format("Could not find file: %s. (did you build the project?)", get_project_executable_path().characters()), "Error", GUI::MessageBox::Type::Error);
  456. return;
  457. }
  458. if (Debugger::the().session()) {
  459. GUI::MessageBox::show(window(), "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  460. return;
  461. }
  462. Debugger::the().set_executable_path(get_project_executable_path());
  463. m_debugger_thread = adopt(*new LibThread::Thread(Debugger::start_static));
  464. m_debugger_thread->start();
  465. });
  466. }
  467. void HackStudioWidget::initialize_debugger()
  468. {
  469. Debugger::initialize(
  470. [this](const PtraceRegisters& regs) {
  471. ASSERT(Debugger::the().session());
  472. const auto& debug_session = *Debugger::the().session();
  473. auto source_position = debug_session.debug_info().get_source_position(regs.eip);
  474. if (!source_position.has_value()) {
  475. dbg() << "Could not find source position for address: " << (void*)regs.eip;
  476. return Debugger::HasControlPassedToUser::No;
  477. }
  478. Core::EventLoop::main().post_event(
  479. *window(),
  480. make<Core::DeferredInvocationEvent>(
  481. [this, source_position, &regs](auto&) {
  482. m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  483. m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  484. m_debug_info_widget->update_state(*Debugger::the().session(), regs);
  485. m_debug_info_widget->set_debug_actions_enabled(true);
  486. m_disassembly_widget->update_state(*Debugger::the().session(), regs);
  487. HackStudioWidget::reveal_action_tab(*m_debug_info_widget);
  488. }));
  489. Core::EventLoop::wake();
  490. return Debugger::HasControlPassedToUser::Yes;
  491. },
  492. [this]() {
  493. Core::EventLoop::main().post_event(*window(), make<Core::DeferredInvocationEvent>([this](auto&) {
  494. m_debug_info_widget->set_debug_actions_enabled(false);
  495. if (m_current_editor_in_execution) {
  496. m_current_editor_in_execution->editor().clear_execution_position();
  497. }
  498. }));
  499. Core::EventLoop::wake();
  500. },
  501. [this]() {
  502. Core::EventLoop::main().post_event(*window(), make<Core::DeferredInvocationEvent>([this](auto&) {
  503. m_debug_info_widget->program_stopped();
  504. m_disassembly_widget->program_stopped();
  505. HackStudioWidget::hide_action_tabs();
  506. GUI::MessageBox::show(window(), "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  507. }));
  508. Core::EventLoop::wake();
  509. });
  510. }
  511. String HackStudioWidget::get_full_path_of_serenity_source(const String& file)
  512. {
  513. auto path_parts = LexicalPath(file).parts();
  514. ASSERT(path_parts[0] == "..");
  515. path_parts.remove(0);
  516. StringBuilder relative_path_builder;
  517. relative_path_builder.join("/", path_parts);
  518. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  519. LexicalPath serenity_sources_base(SERENITY_LIBS_PREFIX);
  520. return String::format("%s/%s", serenity_sources_base.string().characters(), relative_path_builder.to_string().characters());
  521. }
  522. NonnullRefPtr<EditorWrapper> HackStudioWidget::get_editor_of_file(const String& file_name)
  523. {
  524. String file_path = file_name;
  525. // TODO: We can probably do a more specific condition here, something like
  526. // "if (file.starts_with("../Libraries/") || file.starts_with("../AK/"))"
  527. if (file_name.starts_with("../")) {
  528. file_path = get_full_path_of_serenity_source(file_name);
  529. }
  530. open_file(file_path);
  531. return current_editor_wrapper();
  532. }
  533. String HackStudioWidget::get_project_executable_path() const
  534. {
  535. // e.g /my/project.hsp => /my/project
  536. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  537. return m_project->path().substring(0, m_project->path().index_of(".").value());
  538. }
  539. void HackStudioWidget::build(TerminalWrapper& wrapper)
  540. {
  541. if (m_project->type() == ProjectType::JavaScript && m_currently_open_file.ends_with(".js"))
  542. wrapper.run_command(String::format("js -A %s", m_currently_open_file.characters()));
  543. else
  544. wrapper.run_command("make");
  545. }
  546. void HackStudioWidget::run(TerminalWrapper& wrapper)
  547. {
  548. if (m_project->type() == ProjectType::JavaScript && m_currently_open_file.ends_with(".js"))
  549. wrapper.run_command(String::format("js %s", m_currently_open_file.characters()));
  550. else
  551. wrapper.run_command("make run");
  552. }
  553. void HackStudioWidget::hide_action_tabs()
  554. {
  555. m_action_tab_widget->set_preferred_size(0, 24);
  556. };
  557. Project& HackStudioWidget::project()
  558. {
  559. return *m_project;
  560. }
  561. void HackStudioWidget::set_current_editor_wrapper(RefPtr<EditorWrapper> editor_wrapper)
  562. {
  563. m_current_editor_wrapper = editor_wrapper;
  564. }
  565. void HackStudioWidget::create_project_tree_view(GUI::Widget& parent)
  566. {
  567. m_project_tree_view = parent.add<GUI::TreeView>();
  568. m_project_tree_view->set_model(m_project->model());
  569. m_project_tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  570. m_project_tree_view->set_preferred_size(140, 0);
  571. m_project_tree_view->toggle_index(m_project_tree_view->model()->index(0, 0));
  572. m_project_tree_view->on_context_menu_request = [this](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  573. if (index.is_valid()) {
  574. m_project_tree_view_context_menu->popup(event.screen_position(), m_open_selected_action);
  575. }
  576. };
  577. m_project_tree_view->on_selection_change = [this] {
  578. m_open_selected_action->set_enabled(!m_project_tree_view->selection().is_empty());
  579. m_delete_action->set_enabled(!m_project_tree_view->selection().is_empty());
  580. };
  581. m_project_tree_view->on_activation = [this](auto& index) {
  582. auto filename = index.data(GUI::ModelRole::Custom).to_string();
  583. open_file(filename);
  584. };
  585. }
  586. void HackStudioWidget::create_form_editor(GUI::Widget& parent)
  587. {
  588. m_form_inner_container = parent.add<GUI::Widget>();
  589. m_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
  590. auto& form_widgets_toolbar = m_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
  591. form_widgets_toolbar.set_preferred_size(38, 0);
  592. GUI::ActionGroup tool_actions;
  593. tool_actions.set_exclusive(true);
  594. auto cursor_tool_action = GUI::Action::create_checkable("Cursor", Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Cursor.png"), [this](auto&) {
  595. m_form_editor_widget->set_tool(make<CursorTool>(*m_form_editor_widget));
  596. });
  597. cursor_tool_action->set_checked(true);
  598. tool_actions.add_action(cursor_tool_action);
  599. form_widgets_toolbar.add_action(cursor_tool_action);
  600. GUI::WidgetClassRegistration::for_each([&, this](const GUI::WidgetClassRegistration& reg) {
  601. auto icon_path = String::format("/res/icons/hackstudio/G%s.png", reg.class_name().characters());
  602. auto action = GUI::Action::create_checkable(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg, this](auto&) {
  603. m_form_editor_widget->set_tool(make<WidgetTool>(*m_form_editor_widget, reg));
  604. auto widget = reg.construct();
  605. m_form_editor_widget->form_widget().add_child(widget);
  606. widget->set_relative_rect(30, 30, 30, 30);
  607. m_form_editor_widget->model().update();
  608. });
  609. action->set_checked(false);
  610. tool_actions.add_action(action);
  611. form_widgets_toolbar.add_action(move(action));
  612. });
  613. auto& form_editor_inner_splitter = m_form_inner_container->add<GUI::HorizontalSplitter>();
  614. m_form_editor_widget = form_editor_inner_splitter.add<FormEditorWidget>();
  615. auto& form_editing_pane_container = form_editor_inner_splitter.add<GUI::VerticalSplitter>();
  616. form_editing_pane_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  617. form_editing_pane_container.set_preferred_size(190, 0);
  618. form_editing_pane_container.set_layout<GUI::VerticalBoxLayout>();
  619. auto add_properties_pane = [&](auto& text, auto& pane_widget) {
  620. auto& wrapper = form_editing_pane_container.add<GUI::Widget>();
  621. wrapper.set_layout<GUI::VerticalBoxLayout>();
  622. auto& label = wrapper.add<GUI::Label>(text);
  623. label.set_fill_with_background_color(true);
  624. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  625. label.set_font(Gfx::Font::default_bold_font());
  626. label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  627. label.set_preferred_size(0, 16);
  628. wrapper.add_child(pane_widget);
  629. };
  630. m_form_widget_tree_view = GUI::TreeView::construct();
  631. m_form_widget_tree_view->set_model(m_form_editor_widget->model());
  632. m_form_widget_tree_view->on_selection_change = [this] {
  633. m_form_editor_widget->selection().disable_hooks();
  634. m_form_editor_widget->selection().clear();
  635. m_form_widget_tree_view->selection().for_each_index([this](auto& index) {
  636. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  637. // since that would allow you to drag-move the FormWidget.
  638. if (index.internal_data() != &m_form_editor_widget->form_widget())
  639. m_form_editor_widget->selection().add(*(GUI::Widget*)index.internal_data());
  640. });
  641. m_form_editor_widget->update();
  642. m_form_editor_widget->selection().enable_hooks();
  643. };
  644. m_form_editor_widget->selection().on_add = [this](auto& widget) {
  645. m_form_widget_tree_view->selection().add(m_form_editor_widget->model().index_for_widget(widget));
  646. };
  647. m_form_editor_widget->selection().on_remove = [this](auto& widget) {
  648. m_form_widget_tree_view->selection().remove(m_form_editor_widget->model().index_for_widget(widget));
  649. };
  650. m_form_editor_widget->selection().on_clear = [this] {
  651. m_form_widget_tree_view->selection().clear();
  652. };
  653. add_properties_pane("Form widget tree:", *m_form_widget_tree_view);
  654. add_properties_pane("Widget properties:", *GUI::TableView::construct());
  655. }
  656. void HackStudioWidget::create_toolbar(GUI::Widget& parent)
  657. {
  658. auto& toolbar = parent.add<GUI::ToolBar>();
  659. toolbar.add_action(*m_new_action);
  660. toolbar.add_action(*m_add_existing_file_action);
  661. toolbar.add_action(*m_save_action);
  662. toolbar.add_action(*m_delete_action);
  663. toolbar.add_separator();
  664. toolbar.add_action(GUI::CommonActions::make_cut_action([this](auto&) { current_editor().cut_action().activate(); }));
  665. toolbar.add_action(GUI::CommonActions::make_copy_action([this](auto&) { current_editor().copy_action().activate(); }));
  666. toolbar.add_action(GUI::CommonActions::make_paste_action([this](auto&) { current_editor().paste_action().activate(); }));
  667. toolbar.add_separator();
  668. toolbar.add_action(GUI::CommonActions::make_undo_action([this](auto&) { current_editor().undo_action().activate(); }));
  669. toolbar.add_action(GUI::CommonActions::make_redo_action([this](auto&) { current_editor().redo_action().activate(); }));
  670. toolbar.add_separator();
  671. toolbar.add_action(*m_build_action);
  672. toolbar.add_separator();
  673. toolbar.add_action(*m_run_action);
  674. toolbar.add_action(*m_stop_action);
  675. toolbar.add_separator();
  676. toolbar.add_action(*m_debug_action);
  677. }
  678. NonnullRefPtr<GUI::Action> HackStudioWidget::create_build_action()
  679. {
  680. return GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [this](auto&) {
  681. reveal_action_tab(*m_terminal_wrapper);
  682. build(*m_terminal_wrapper);
  683. m_stop_action->set_enabled(true);
  684. });
  685. }
  686. NonnullRefPtr<GUI::Action> HackStudioWidget::create_run_action()
  687. {
  688. return GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/program-run.png"), [this](auto&) {
  689. reveal_action_tab(*m_terminal_wrapper);
  690. run(*m_terminal_wrapper);
  691. m_stop_action->set_enabled(true);
  692. });
  693. }
  694. void HackStudioWidget::create_action_tab(GUI::Widget& parent)
  695. {
  696. m_action_tab_widget = parent.add<GUI::TabWidget>();
  697. m_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  698. m_action_tab_widget->set_preferred_size(0, 24);
  699. m_action_tab_widget->on_change = [this](auto&) {
  700. on_action_tab_change();
  701. static bool first_time = true;
  702. if (!first_time)
  703. m_action_tab_widget->set_preferred_size(0, 200);
  704. first_time = false;
  705. };
  706. m_find_in_files_widget = m_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  707. m_terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Build", false);
  708. m_debug_info_widget = m_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  709. m_disassembly_widget = m_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  710. m_git_widget = m_action_tab_widget->add_tab<GitWidget>("Git", LexicalPath(m_project->root_directory()));
  711. m_git_widget->set_view_diff_callback([this](const auto& original_content, const auto& diff) {
  712. m_diff_viewer->set_content(original_content, diff);
  713. set_edit_mode(EditMode::Diff);
  714. });
  715. }
  716. void HackStudioWidget::create_app_menubar(GUI::MenuBar& menubar)
  717. {
  718. auto& app_menu = menubar.add_menu("HackStudio");
  719. app_menu.add_action(*m_open_action);
  720. app_menu.add_action(*m_save_action);
  721. app_menu.add_separator();
  722. app_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  723. GUI::Application::the()->quit();
  724. }));
  725. }
  726. void HackStudioWidget::create_project_menubar(GUI::MenuBar& menubar)
  727. {
  728. auto& project_menu = menubar.add_menu("Project");
  729. project_menu.add_action(*m_new_action);
  730. project_menu.add_action(*m_add_existing_file_action);
  731. }
  732. void HackStudioWidget::create_edit_menubar(GUI::MenuBar& menubar)
  733. {
  734. auto& edit_menu = menubar.add_menu("Edit");
  735. 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"), [this](auto&) {
  736. reveal_action_tab(*m_find_in_files_widget);
  737. m_find_in_files_widget->focus_textbox_and_select_all();
  738. }));
  739. }
  740. void HackStudioWidget::create_build_menubar(GUI::MenuBar& menubar)
  741. {
  742. auto& build_menu = menubar.add_menu("Build");
  743. build_menu.add_action(*m_build_action);
  744. build_menu.add_separator();
  745. build_menu.add_action(*m_run_action);
  746. build_menu.add_action(*m_stop_action);
  747. build_menu.add_separator();
  748. build_menu.add_action(*m_debug_action);
  749. }
  750. void HackStudioWidget::create_view_menubar(GUI::MenuBar& menubar)
  751. {
  752. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [this](auto&) {
  753. hide_action_tabs();
  754. });
  755. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [this](auto&) {
  756. m_locator->open();
  757. });
  758. auto& view_menu = menubar.add_menu("View");
  759. view_menu.add_action(hide_action_tabs_action);
  760. view_menu.add_action(open_locator_action);
  761. view_menu.add_separator();
  762. view_menu.add_action(*m_add_editor_action);
  763. view_menu.add_action(*m_remove_current_editor_action);
  764. view_menu.add_action(*m_add_terminal_action);
  765. view_menu.add_action(*m_remove_current_terminal_action);
  766. }
  767. void HackStudioWidget::create_help_menubar(GUI::MenuBar& menubar)
  768. {
  769. auto& help_menu = menubar.add_menu("Help");
  770. help_menu.add_action(GUI::Action::create("About", [this](auto&) {
  771. GUI::AboutDialog::show("HackStudio", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"), window());
  772. }));
  773. }
  774. NonnullRefPtr<GUI::Action> HackStudioWidget::create_stop_action()
  775. {
  776. auto action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/program-stop.png"), [this](auto&) {
  777. m_terminal_wrapper->kill_running_command();
  778. });
  779. action->set_enabled(false);
  780. return action;
  781. }
  782. void HackStudioWidget::initialize_menubar(GUI::MenuBar& menubar)
  783. {
  784. create_app_menubar(menubar);
  785. create_project_menubar(menubar);
  786. create_edit_menubar(menubar);
  787. create_build_menubar(menubar);
  788. create_view_menubar(menubar);
  789. create_help_menubar(menubar);
  790. }
  791. }