main.cpp 32 KB

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