main.cpp 17 KB

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