summaryrefslogtreecommitdiff
path: root/Applications/Help/main.cpp
blob: dd20b5120553ebc1e88a12382965fc666194d640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "ManualModel.h"
#include <LibCore/CFile.h>
#include <LibDraw/PNGLoader.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GMessageBox.h>
#include <LibGUI/GSplitter.h>
#include <LibGUI/GTextEditor.h>
#include <LibGUI/GTreeView.h>
#include <LibGUI/GWindow.h>
#include <LibHTML/HtmlView.h>
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/Parser/CSSParser.h>
#include <LibHTML/Parser/HTMLParser.h>
#include <LibMarkdown/MDDocument.h>

int main(int argc, char* argv[])
{
    GApplication app(argc, argv);

    auto window = GWindow::construct();
    window->set_title("Help");
    window->set_rect(300, 200, 570, 500);

    auto splitter = GSplitter::construct(Orientation::Horizontal, nullptr);

    auto model = ManualModel::create();

    auto tree_view = GTreeView::construct(splitter);
    tree_view->set_model(model);
    tree_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
    tree_view->set_preferred_size(200, 500);

    auto html_view = HtmlView::construct(splitter);

    extern const char default_stylesheet_source[];
    String css = default_stylesheet_source;
    auto sheet = parse_css(css);

    tree_view->on_selection_change = [&] {
        String path = model->page_path(tree_view->selection().first());
        if (path.is_null()) {
            html_view->set_document(nullptr);
            return;
        }

        dbg() << "Opening page at " << path;

        auto file = CFile::construct();
        file->set_filename(path);

        if (!file->open(CIODevice::OpenMode::ReadOnly)) {
            int saved_errno = errno;
            GMessageBox::show(strerror(saved_errno), "Failed to open man page", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
            return;
        }
        auto buffer = file->read_all();
        StringView source { (char*)buffer.data(), buffer.size() };

        MDDocument md_document;
        bool success = md_document.parse(source);
        ASSERT(success);

        String html = md_document.render_to_html();
        auto html_document = parse_html(html);
        html_document->normalize();
        html_document->add_sheet(sheet);
        html_view->set_document(html_document);

        String page_and_section = model->page_and_section(tree_view->selection().first());
        window->set_title(String::format("Help: %s", page_and_section.characters()));
    };

    window->set_main_widget(splitter);
    window->show();

    window->set_icon(load_png("/res/icons/16x16/book.png"));

    return app.exec();
}