main.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include "CppLexer.h"
  2. #include "Editor.h"
  3. #include "EditorWrapper.h"
  4. #include "FindInFilesWidget.h"
  5. #include "FormEditorWidget.h"
  6. #include "Locator.h"
  7. #include "Project.h"
  8. #include "TerminalWrapper.h"
  9. #include <LibCore/CFile.h>
  10. #include <LibGUI/GAboutDialog.h>
  11. #include <LibGUI/GAction.h>
  12. #include <LibGUI/GApplication.h>
  13. #include <LibGUI/GBoxLayout.h>
  14. #include <LibGUI/GButton.h>
  15. #include <LibGUI/GFilePicker.h>
  16. #include <LibGUI/GInputBox.h>
  17. #include <LibGUI/GLabel.h>
  18. #include <LibGUI/GListView.h>
  19. #include <LibGUI/GMenu.h>
  20. #include <LibGUI/GMenuBar.h>
  21. #include <LibGUI/GMessageBox.h>
  22. #include <LibGUI/GSplitter.h>
  23. #include <LibGUI/GStackWidget.h>
  24. #include <LibGUI/GTabWidget.h>
  25. #include <LibGUI/GTableView.h>
  26. #include <LibGUI/GTextBox.h>
  27. #include <LibGUI/GTextEditor.h>
  28. #include <LibGUI/GToolBar.h>
  29. #include <LibGUI/GTreeView.h>
  30. #include <LibGUI/GWidget.h>
  31. #include <LibGUI/GWindow.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. NonnullRefPtrVector<EditorWrapper> g_all_editor_wrappers;
  35. RefPtr<EditorWrapper> g_current_editor_wrapper;
  36. String g_currently_open_file;
  37. OwnPtr<Project> g_project;
  38. RefPtr<GWindow> g_window;
  39. RefPtr<GListView> g_project_list_view;
  40. RefPtr<GStackWidget> g_right_hand_stack;
  41. RefPtr<GSplitter> g_text_inner_splitter;
  42. RefPtr<GWidget> g_form_inner_container;
  43. RefPtr<FormEditorWidget> g_form_editor_widget;
  44. static RefPtr<GTabWidget> s_action_tab_widget;
  45. void add_new_editor(GWidget& parent)
  46. {
  47. auto wrapper = EditorWrapper::construct(nullptr);
  48. if (s_action_tab_widget) {
  49. parent.insert_child_before(wrapper, *s_action_tab_widget);
  50. } else {
  51. parent.add_child(wrapper);
  52. }
  53. g_current_editor_wrapper = wrapper;
  54. g_all_editor_wrappers.append(wrapper);
  55. wrapper->editor().set_focus(true);
  56. }
  57. enum class EditMode {
  58. Text,
  59. Form,
  60. };
  61. void set_edit_mode(EditMode mode)
  62. {
  63. if (mode == EditMode::Text) {
  64. g_right_hand_stack->set_active_widget(g_text_inner_splitter);
  65. } else if (mode == EditMode::Form) {
  66. g_right_hand_stack->set_active_widget(g_form_inner_container);
  67. }
  68. }
  69. EditorWrapper& current_editor_wrapper()
  70. {
  71. ASSERT(g_current_editor_wrapper);
  72. return *g_current_editor_wrapper;
  73. }
  74. Editor& current_editor()
  75. {
  76. return current_editor_wrapper().editor();
  77. }
  78. static void build(TerminalWrapper&);
  79. static void run(TerminalWrapper&);
  80. void open_file(const String&);
  81. int main(int argc, char** argv)
  82. {
  83. GApplication app(argc, argv);
  84. Function<void()> update_actions;
  85. g_window = GWindow::construct();
  86. g_window->set_rect(100, 100, 800, 600);
  87. g_window->set_title("HackStudio");
  88. auto widget = GWidget::construct();
  89. g_window->set_main_widget(widget);
  90. widget->set_fill_with_background_color(true);
  91. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  92. widget->layout()->set_spacing(0);
  93. if (chdir("/home/anon/little") < 0) {
  94. perror("chdir");
  95. return 1;
  96. }
  97. g_project = Project::load_from_file("little.files");
  98. ASSERT(g_project);
  99. auto toolbar = GToolBar::construct(widget);
  100. auto outer_splitter = GSplitter::construct(Orientation::Horizontal, widget);
  101. g_project_list_view = GListView::construct(outer_splitter);
  102. g_project_list_view->set_model(g_project->model());
  103. g_project_list_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  104. g_project_list_view->set_preferred_size(140, 0);
  105. g_right_hand_stack = GStackWidget::construct(outer_splitter);
  106. g_form_inner_container = GWidget::construct(g_right_hand_stack);
  107. g_form_inner_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  108. auto form_widgets_toolbar = GToolBar::construct(Orientation::Vertical, 26, g_form_inner_container);
  109. form_widgets_toolbar->set_preferred_size(38, 0);
  110. form_widgets_toolbar->add_action(GAction::create("Cursor", GraphicsBitmap::load_from_file("/res/cursors/arrow.png"), [&](auto&) {
  111. }));
  112. GWidgetClassRegistration::for_each([&](const GWidgetClassRegistration& reg) {
  113. auto icon_path = String::format("/res/icons/widgets/%s.png", reg.class_name().characters());
  114. auto action = GAction::create(reg.class_name(), GraphicsBitmap::load_from_file(icon_path), [&](auto&) {
  115. });
  116. form_widgets_toolbar->add_action(move(action));
  117. });
  118. auto form_editor_inner_splitter = GSplitter::construct(Orientation::Horizontal, g_form_inner_container);
  119. g_form_editor_widget = FormEditorWidget::construct(form_editor_inner_splitter);
  120. auto form_editing_pane_container = GSplitter::construct(Orientation::Vertical, form_editor_inner_splitter);
  121. form_editing_pane_container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  122. form_editing_pane_container->set_preferred_size(170, 0);
  123. form_editing_pane_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  124. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  125. auto wrapper = GWidget::construct(form_editing_pane_container.ptr());
  126. wrapper->set_layout(make<GBoxLayout>(Orientation::Vertical));
  127. auto label = GLabel::construct(text, wrapper);
  128. label->set_fill_with_background_color(true);
  129. label->set_text_alignment(TextAlignment::CenterLeft);
  130. label->set_font(Font::default_bold_font());
  131. label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  132. label->set_preferred_size(0, 16);
  133. wrapper->add_child(pane_widget);
  134. };
  135. add_properties_pane("Form widget tree:", GTreeView::construct(nullptr));
  136. add_properties_pane("Widget properties:", GTableView::construct(nullptr));
  137. g_text_inner_splitter = GSplitter::construct(Orientation::Vertical, g_right_hand_stack);
  138. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  139. add_new_editor(*g_text_inner_splitter);
  140. auto new_action = GAction::create("Add new file to project...", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GAction&) {
  141. auto input_box = GInputBox::construct("Enter name of new file:", "Add new file to project", g_window);
  142. if (input_box->exec() == GInputBox::ExecCancel)
  143. return;
  144. auto filename = input_box->text_value();
  145. auto file = CFile::construct(filename);
  146. if (!file->open((CIODevice::OpenMode)(CIODevice::WriteOnly | CIODevice::MustBeNew))) {
  147. GMessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  148. return;
  149. }
  150. if (!g_project->add_file(filename)) {
  151. GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  152. // FIXME: Should we unlink the file here maybe?
  153. return;
  154. }
  155. open_file(filename);
  156. });
  157. auto add_existing_file_action = GAction::create("Add existing file to project...", GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  158. auto result = GFilePicker::get_open_filepath("Add existing file to project");
  159. if (!result.has_value())
  160. return;
  161. auto& filename = result.value();
  162. if (!g_project->add_file(filename)) {
  163. GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
  164. return;
  165. }
  166. open_file(filename);
  167. });
  168. auto switch_to_next_editor = GAction::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  169. if (g_all_editor_wrappers.size() <= 1)
  170. return;
  171. Vector<EditorWrapper*> wrappers;
  172. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  173. wrappers.append(&child);
  174. return IterationDecision::Continue;
  175. });
  176. for (int i = 0; i < wrappers.size(); ++i) {
  177. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  178. if (i == wrappers.size() - 1)
  179. wrappers[0]->editor().set_focus(true);
  180. else
  181. wrappers[i + 1]->editor().set_focus(true);
  182. }
  183. }
  184. });
  185. auto switch_to_previous_editor = GAction::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  186. if (g_all_editor_wrappers.size() <= 1)
  187. return;
  188. Vector<EditorWrapper*> wrappers;
  189. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  190. wrappers.append(&child);
  191. return IterationDecision::Continue;
  192. });
  193. for (int i = wrappers.size() - 1; i >= 0; --i) {
  194. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  195. if (i == 0)
  196. wrappers.last()->editor().set_focus(true);
  197. else
  198. wrappers[i - 1]->editor().set_focus(true);
  199. }
  200. }
  201. });
  202. auto remove_current_editor_action = GAction::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  203. if (g_all_editor_wrappers.size() <= 1)
  204. return;
  205. auto wrapper = g_current_editor_wrapper;
  206. switch_to_next_editor->activate();
  207. g_text_inner_splitter->remove_child(*wrapper);
  208. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  209. update_actions();
  210. });
  211. auto save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  212. if (g_currently_open_file.is_empty())
  213. return;
  214. current_editor().write_to_file(g_currently_open_file);
  215. });
  216. toolbar->add_action(new_action);
  217. toolbar->add_action(add_existing_file_action);
  218. toolbar->add_action(save_action);
  219. toolbar->add_separator();
  220. toolbar->add_action(GCommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  221. toolbar->add_action(GCommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  222. toolbar->add_action(GCommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  223. toolbar->add_separator();
  224. toolbar->add_action(GCommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  225. toolbar->add_action(GCommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  226. toolbar->add_separator();
  227. g_project_list_view->on_activation = [&](auto& index) {
  228. auto filename = g_project_list_view->model()->data(index).to_string();
  229. open_file(filename);
  230. };
  231. s_action_tab_widget = GTabWidget::construct(g_text_inner_splitter);
  232. s_action_tab_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  233. s_action_tab_widget->set_preferred_size(0, 24);
  234. auto reveal_action_tab = [&](auto& widget) {
  235. if (s_action_tab_widget->preferred_size().height() < 200)
  236. s_action_tab_widget->set_preferred_size(0, 200);
  237. s_action_tab_widget->set_active_widget(widget);
  238. };
  239. auto hide_action_tabs = [&] {
  240. s_action_tab_widget->set_preferred_size(0, 24);
  241. };
  242. auto hide_action_tabs_action = GAction::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  243. hide_action_tabs();
  244. });
  245. auto add_editor_action = GAction::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E }, [&](auto&) {
  246. add_new_editor(*g_text_inner_splitter);
  247. update_actions();
  248. });
  249. auto find_in_files_widget = FindInFilesWidget::construct(nullptr);
  250. s_action_tab_widget->add_widget("Find in files", find_in_files_widget);
  251. auto terminal_wrapper = TerminalWrapper::construct(nullptr);
  252. s_action_tab_widget->add_widget("Console", terminal_wrapper);
  253. auto locator = Locator::construct(widget);
  254. auto open_locator_action = GAction::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  255. locator->open();
  256. });
  257. auto menubar = make<GMenuBar>();
  258. auto app_menu = make<GMenu>("HackStudio");
  259. app_menu->add_action(save_action);
  260. app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
  261. app.quit();
  262. }));
  263. menubar->add_menu(move(app_menu));
  264. auto project_menu = make<GMenu>("Project");
  265. project_menu->add_action(new_action);
  266. project_menu->add_action(add_existing_file_action);
  267. menubar->add_menu(move(project_menu));
  268. auto edit_menu = make<GMenu>("Edit");
  269. edit_menu->add_action(GAction::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
  270. reveal_action_tab(find_in_files_widget);
  271. find_in_files_widget->focus_textbox_and_select_all();
  272. }));
  273. menubar->add_menu(move(edit_menu));
  274. auto build_action = GAction::create("Build", { Mod_Ctrl, Key_B }, GraphicsBitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  275. reveal_action_tab(terminal_wrapper);
  276. build(terminal_wrapper);
  277. });
  278. toolbar->add_action(build_action);
  279. auto run_action = GAction::create("Run", { Mod_Ctrl, Key_R }, GraphicsBitmap::load_from_file("/res/icons/16x16/run.png"), [&](auto&) {
  280. reveal_action_tab(terminal_wrapper);
  281. run(terminal_wrapper);
  282. });
  283. toolbar->add_action(run_action);
  284. auto build_menu = make<GMenu>("Build");
  285. build_menu->add_action(build_action);
  286. build_menu->add_action(run_action);
  287. menubar->add_menu(move(build_menu));
  288. auto view_menu = make<GMenu>("View");
  289. view_menu->add_action(hide_action_tabs_action);
  290. view_menu->add_action(open_locator_action);
  291. view_menu->add_separator();
  292. view_menu->add_action(add_editor_action);
  293. view_menu->add_action(remove_current_editor_action);
  294. menubar->add_menu(move(view_menu));
  295. auto small_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
  296. auto help_menu = make<GMenu>("Help");
  297. help_menu->add_action(GAction::create("About", [&](auto&) {
  298. GAboutDialog::show("HackStudio", small_icon, g_window);
  299. }));
  300. menubar->add_menu(move(help_menu));
  301. app.set_menubar(move(menubar));
  302. g_window->set_icon(small_icon);
  303. g_window->show();
  304. update_actions = [&]() {
  305. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  306. };
  307. open_file("test.frm");
  308. update_actions();
  309. return app.exec();
  310. }
  311. void build(TerminalWrapper& wrapper)
  312. {
  313. wrapper.run_command("make");
  314. }
  315. void run(TerminalWrapper& wrapper)
  316. {
  317. wrapper.run_command("make run");
  318. }
  319. struct TextStyle {
  320. Color color;
  321. const Font* font { nullptr };
  322. };
  323. static TextStyle style_for_token_type(CppToken::Type type)
  324. {
  325. switch (type) {
  326. case CppToken::Type::Keyword:
  327. return { Color::Black, &Font::default_bold_fixed_width_font() };
  328. case CppToken::Type::KnownType:
  329. return { Color::from_rgb(0x929200), &Font::default_bold_fixed_width_font() };
  330. case CppToken::Type::Identifier:
  331. return { Color::from_rgb(0x000092) };
  332. case CppToken::Type::DoubleQuotedString:
  333. case CppToken::Type::SingleQuotedString:
  334. case CppToken::Type::Number:
  335. return { Color::from_rgb(0x920000) };
  336. case CppToken::Type::PreprocessorStatement:
  337. return { Color::from_rgb(0x009292) };
  338. case CppToken::Type::Comment:
  339. return { Color::from_rgb(0x009200) };
  340. default:
  341. return { Color::Black };
  342. }
  343. }
  344. static void rehighlight()
  345. {
  346. auto text = current_editor().text();
  347. CppLexer lexer(text);
  348. auto tokens = lexer.lex();
  349. Vector<GTextDocumentSpan> spans;
  350. for (auto& token : tokens) {
  351. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  352. dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
  353. #endif
  354. GTextDocumentSpan span;
  355. span.range.set_start({ token.m_start.line, token.m_start.column });
  356. span.range.set_end({ token.m_end.line, token.m_end.column });
  357. auto style = style_for_token_type(token.m_type);
  358. span.color = style.color;
  359. span.font = style.font;
  360. spans.append(span);
  361. }
  362. current_editor().document().set_spans(spans);
  363. current_editor().update();
  364. }
  365. void open_file(const String& filename)
  366. {
  367. auto file = g_project->get_file(filename);
  368. current_editor().set_document(const_cast<GTextDocument&>(file->document()));
  369. if (filename.ends_with(".cpp") || filename.ends_with(".h")) {
  370. current_editor().on_change = [] { rehighlight(); };
  371. rehighlight();
  372. } else {
  373. current_editor().on_change = nullptr;
  374. }
  375. if (filename.ends_with(".frm")) {
  376. set_edit_mode(EditMode::Form);
  377. } else {
  378. set_edit_mode(EditMode::Text);
  379. }
  380. g_currently_open_file = filename;
  381. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  382. g_project_list_view->update();
  383. current_editor_wrapper().filename_label().set_text(filename);
  384. current_editor().set_focus(true);
  385. }