main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "History.h"
  2. #include "ManualModel.h"
  3. #include <LibCore/CFile.h>
  4. #include <LibDraw/PNGLoader.h>
  5. #include <LibGUI/GAboutDialog.h>
  6. #include <LibGUI/GAction.h>
  7. #include <LibGUI/GApplication.h>
  8. #include <LibGUI/GBoxLayout.h>
  9. #include <LibGUI/GMenu.h>
  10. #include <LibGUI/GMenuBar.h>
  11. #include <LibGUI/GMessageBox.h>
  12. #include <LibGUI/GSplitter.h>
  13. #include <LibGUI/GTextEditor.h>
  14. #include <LibGUI/GToolBar.h>
  15. #include <LibGUI/GTreeView.h>
  16. #include <LibGUI/GWindow.h>
  17. #include <LibHTML/HtmlView.h>
  18. #include <LibHTML/Layout/LayoutNode.h>
  19. #include <LibHTML/Parser/CSSParser.h>
  20. #include <LibHTML/Parser/HTMLParser.h>
  21. #include <LibMarkdown/MDDocument.h>
  22. #include <libgen.h>
  23. int main(int argc, char* argv[])
  24. {
  25. GApplication app(argc, argv);
  26. auto window = GWindow::construct();
  27. window->set_title("Help");
  28. window->set_rect(300, 200, 570, 500);
  29. auto widget = GWidget::construct();
  30. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  31. widget->layout()->set_spacing(0);
  32. auto toolbar = GToolBar::construct(widget);
  33. auto splitter = GSplitter::construct(Orientation::Horizontal, widget);
  34. auto model = ManualModel::create();
  35. auto tree_view = GTreeView::construct(splitter);
  36. tree_view->set_model(model);
  37. tree_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  38. tree_view->set_preferred_size(200, 500);
  39. auto html_view = HtmlView::construct(splitter);
  40. History history;
  41. RefPtr<GAction> go_back_action;
  42. RefPtr<GAction> go_forward_action;
  43. auto update_actions = [&]() {
  44. go_back_action->set_enabled(history.can_go_back());
  45. go_forward_action->set_enabled(history.can_go_forward());
  46. };
  47. auto open_page = [&](String path) {
  48. if (path.is_null()) {
  49. html_view->set_document(nullptr);
  50. return;
  51. }
  52. dbg() << "Opening page at " << path;
  53. auto file = CFile::construct();
  54. file->set_filename(path);
  55. if (!file->open(CIODevice::OpenMode::ReadOnly)) {
  56. int saved_errno = errno;
  57. GMessageBox::show(strerror(saved_errno), "Failed to open man page", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
  58. return;
  59. }
  60. auto buffer = file->read_all();
  61. StringView source { (char*)buffer.data(), buffer.size() };
  62. MDDocument md_document;
  63. bool success = md_document.parse(source);
  64. ASSERT(success);
  65. String html = md_document.render_to_html();
  66. auto html_document = parse_html(html);
  67. html_view->set_document(html_document);
  68. String page_and_section = model->page_and_section(tree_view->selection().first());
  69. window->set_title(String::format("Help: %s", page_and_section.characters()));
  70. };
  71. tree_view->on_selection_change = [&] {
  72. String path = model->page_path(tree_view->selection().first());
  73. if (path.is_null()) {
  74. html_view->set_document(nullptr);
  75. return;
  76. }
  77. history.push(path);
  78. update_actions();
  79. open_page(path);
  80. };
  81. html_view->on_link_click = [&](const String& href) {
  82. char* current_path = strdup(history.current().characters());
  83. char* dir_path = dirname(current_path);
  84. char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr);
  85. free(current_path);
  86. history.push(path);
  87. update_actions();
  88. open_page(path);
  89. free(path);
  90. };
  91. go_back_action = GCommonActions::make_go_back_action([&](auto&) {
  92. history.go_back();
  93. update_actions();
  94. open_page(history.current());
  95. });
  96. go_forward_action = GCommonActions::make_go_forward_action([&](auto&) {
  97. history.go_forward();
  98. update_actions();
  99. open_page(history.current());
  100. });
  101. go_back_action->set_enabled(false);
  102. go_forward_action->set_enabled(false);
  103. toolbar->add_action(*go_back_action);
  104. toolbar->add_action(*go_forward_action);
  105. auto menubar = make<GMenuBar>();
  106. auto app_menu = make<GMenu>("Help");
  107. app_menu->add_action(GAction::create("About", [&](const GAction&) {
  108. GAboutDialog::show("Help", load_png("/res/icons/16x16/book.png"), window);
  109. }));
  110. app_menu->add_separator();
  111. app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
  112. GApplication::the().quit(0);
  113. }));
  114. menubar->add_menu(move(app_menu));
  115. auto go_menu = make<GMenu>("Go");
  116. go_menu->add_action(*go_back_action);
  117. go_menu->add_action(*go_forward_action);
  118. menubar->add_menu(move(go_menu));
  119. app.set_menubar(move(menubar));
  120. window->set_main_widget(widget);
  121. window->show();
  122. window->set_icon(load_png("/res/icons/16x16/book.png"));
  123. return app.exec();
  124. }