main.cpp 32 KB

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