main.cpp 33 KB

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