main.cpp 31 KB

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