main.cpp 31 KB

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