main.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "ManualModel.h"
  2. #include <LibCore/CFile.h>
  3. #include <LibDraw/PNGLoader.h>
  4. #include <LibGUI/GApplication.h>
  5. #include <LibGUI/GMessageBox.h>
  6. #include <LibGUI/GSplitter.h>
  7. #include <LibGUI/GTextEditor.h>
  8. #include <LibGUI/GTreeView.h>
  9. #include <LibGUI/GWindow.h>
  10. #include <LibHTML/HtmlView.h>
  11. #include <LibHTML/Layout/LayoutNode.h>
  12. #include <LibHTML/Parser/CSSParser.h>
  13. #include <LibHTML/Parser/HTMLParser.h>
  14. #include <LibMarkdown/MDDocument.h>
  15. int main(int argc, char* argv[])
  16. {
  17. GApplication app(argc, argv);
  18. auto window = GWindow::construct();
  19. window->set_title("Help");
  20. window->set_rect(300, 200, 570, 500);
  21. auto splitter = GSplitter::construct(Orientation::Horizontal, nullptr);
  22. auto model = ManualModel::create();
  23. auto tree_view = GTreeView::construct(splitter);
  24. tree_view->set_model(model);
  25. tree_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  26. tree_view->set_preferred_size(200, 500);
  27. auto html_view = HtmlView::construct(splitter);
  28. extern const char default_stylesheet_source[];
  29. String css = default_stylesheet_source;
  30. auto sheet = parse_css(css);
  31. tree_view->on_selection_change = [&] {
  32. String path = model->page_path(tree_view->selection().first());
  33. if (path.is_null()) {
  34. html_view->set_document(nullptr);
  35. return;
  36. }
  37. dbg() << "Opening page at " << path;
  38. auto file = CFile::construct();
  39. file->set_filename(path);
  40. if (!file->open(CIODevice::OpenMode::ReadOnly)) {
  41. int saved_errno = errno;
  42. GMessageBox::show(strerror(saved_errno), "Failed to open man page", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
  43. return;
  44. }
  45. auto buffer = file->read_all();
  46. StringView source { (char*)buffer.data(), buffer.size() };
  47. MDDocument md_document;
  48. bool success = md_document.parse(source);
  49. ASSERT(success);
  50. String html = md_document.render_to_html();
  51. auto html_document = parse_html(html);
  52. html_document->normalize();
  53. html_document->add_sheet(sheet);
  54. html_view->set_document(html_document);
  55. String page_and_section = model->page_and_section(tree_view->selection().first());
  56. window->set_title(String::format("Help: %s", page_and_section.characters()));
  57. };
  58. window->set_main_widget(splitter);
  59. window->show();
  60. window->set_icon(load_png("/res/icons/16x16/book.png"));
  61. return app.exec();
  62. }