HackStudioWidget.cpp 35 KB

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