main.cpp 18 KB

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