Editor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "Editor.h"
  2. #include "EditorWrapper.h"
  3. #include <AK/FileSystemPath.h>
  4. #include <LibCore/CDirIterator.h>
  5. #include <LibCore/CFile.h>
  6. #include <LibGUI/GApplication.h>
  7. #include <LibGUI/GPainter.h>
  8. #include <LibGUI/GScrollBar.h>
  9. #include <LibGUI/GWindow.h>
  10. #include <LibHTML/DOM/ElementFactory.h>
  11. #include <LibHTML/DOM/HTMLHeadElement.h>
  12. #include <LibHTML/DOM/Text.h>
  13. #include <LibHTML/HtmlView.h>
  14. #include <LibHTML/Parser/HTMLParser.h>
  15. #include <LibMarkdown/MDDocument.h>
  16. //#define EDITOR_DEBUG
  17. Editor::Editor(GWidget* parent)
  18. : GTextEditor(GTextEditor::MultiLine, parent)
  19. {
  20. m_documentation_tooltip_window = GWindow::construct();
  21. m_documentation_tooltip_window->set_rect(0, 0, 500, 400);
  22. m_documentation_tooltip_window->set_window_type(GWindowType::Tooltip);
  23. m_documentation_html_view = HtmlView::construct(nullptr);
  24. m_documentation_tooltip_window->set_main_widget(m_documentation_html_view);
  25. }
  26. Editor::~Editor()
  27. {
  28. }
  29. EditorWrapper& Editor::wrapper()
  30. {
  31. return static_cast<EditorWrapper&>(*parent());
  32. }
  33. const EditorWrapper& Editor::wrapper() const
  34. {
  35. return static_cast<const EditorWrapper&>(*parent());
  36. }
  37. void Editor::focusin_event(CEvent& event)
  38. {
  39. wrapper().set_editor_has_focus({}, true);
  40. if (on_focus)
  41. on_focus();
  42. GTextEditor::focusin_event(event);
  43. }
  44. void Editor::focusout_event(CEvent& event)
  45. {
  46. wrapper().set_editor_has_focus({}, false);
  47. GTextEditor::focusout_event(event);
  48. }
  49. void Editor::paint_event(GPaintEvent& event)
  50. {
  51. GTextEditor::paint_event(event);
  52. if (is_focused()) {
  53. GPainter painter(*this);
  54. painter.add_clip_rect(event.rect());
  55. auto rect = frame_inner_rect();
  56. if (vertical_scrollbar().is_visible())
  57. rect.set_width(rect.width() - vertical_scrollbar().width());
  58. if (horizontal_scrollbar().is_visible())
  59. rect.set_height(rect.height() - horizontal_scrollbar().height());
  60. painter.draw_rect(rect, Color::from_rgb(0x955233));
  61. }
  62. }
  63. static HashMap<String, String>& man_paths()
  64. {
  65. static HashMap<String, String> paths;
  66. if (paths.is_empty()) {
  67. // FIXME: This should also search man3, possibly other places..
  68. CDirIterator it("/usr/share/man/man2", CDirIterator::Flags::SkipDots);
  69. while (it.has_next()) {
  70. auto path = String::format("/usr/share/man/man2/%s", it.next_path().characters());
  71. auto title = FileSystemPath(path).title();
  72. paths.set(title, path);
  73. }
  74. }
  75. return paths;
  76. }
  77. void Editor::show_documentation_tooltip_if_available(const String& hovered_token, const Point& screen_location)
  78. {
  79. auto it = man_paths().find(hovered_token);
  80. if (it == man_paths().end()) {
  81. #ifdef EDITOR_DEBUG
  82. dbg() << "no man path for " << hovered_token;
  83. #endif
  84. m_documentation_tooltip_window->hide();
  85. return;
  86. }
  87. if (m_documentation_tooltip_window->is_visible() && hovered_token == m_last_parsed_token) {
  88. return;
  89. }
  90. #ifdef EDITOR_DEBUG
  91. dbg() << "opening " << it->value;
  92. #endif
  93. auto file = CFile::construct(it->value);
  94. if (!file->open(CFile::ReadOnly)) {
  95. dbg() << "failed to open " << it->value << " " << file->error_string();
  96. return;
  97. }
  98. MDDocument man_document;
  99. bool success = man_document.parse(file->read_all());
  100. if (!success) {
  101. dbg() << "failed to parse markdown";
  102. return;
  103. }
  104. auto html_text = man_document.render_to_html();
  105. auto html_document = parse_html(html_text);
  106. if (!html_document) {
  107. dbg() << "failed to parse HTML";
  108. return;
  109. }
  110. // FIXME: LibHTML needs a friendlier DOM manipulation API. Something like innerHTML :^)
  111. auto style_element = create_element(*html_document, "style");
  112. style_element->append_child(adopt(*new Text(*html_document, "body { background-color: #dac7b5; }")));
  113. // FIXME: This const_cast should not be necessary.
  114. auto* head_element = const_cast<HTMLHeadElement*>(html_document->head());
  115. ASSERT(head_element);
  116. head_element->append_child(style_element);
  117. m_documentation_html_view->set_document(html_document);
  118. m_documentation_tooltip_window->move_to(screen_location.translated(4, 4));
  119. m_documentation_tooltip_window->show();
  120. m_last_parsed_token = hovered_token;
  121. }
  122. void Editor::mousemove_event(GMouseEvent& event)
  123. {
  124. GTextEditor::mousemove_event(event);
  125. if (document().spans().is_empty())
  126. return;
  127. auto text_position = text_position_at(event.position());
  128. if (!text_position.is_valid()) {
  129. GApplication::the().hide_tooltip();
  130. return;
  131. }
  132. for (auto& span : document().spans()) {
  133. if (span.range.contains(text_position)) {
  134. auto adjusted_range = span.range;
  135. adjusted_range.end().set_column(adjusted_range.end().column() + 1);
  136. auto hovered_span_text = document().text_in_range(adjusted_range);
  137. #ifdef EDITOR_DEBUG
  138. dbg() << "Hovering: " << adjusted_range << " \"" << hovered_span_text << "\"";
  139. #endif
  140. show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location()));
  141. return;
  142. }
  143. }
  144. GApplication::the().hide_tooltip();
  145. }