Editor.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Editor.h"
  27. #include "EditorWrapper.h"
  28. #include <AK/ByteBuffer.h>
  29. #include <AK/FileSystemPath.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/File.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/Painter.h>
  34. #include <LibGUI/ScrollBar.h>
  35. #include <LibGUI/Window.h>
  36. #include <LibHTML/DOM/ElementFactory.h>
  37. #include <LibHTML/DOM/HTMLHeadElement.h>
  38. #include <LibHTML/DOM/Text.h>
  39. #include <LibHTML/HtmlView.h>
  40. #include <LibHTML/Parser/HTMLParser.h>
  41. #include <LibMarkdown/MDDocument.h>
  42. //#define EDITOR_DEBUG
  43. Editor::Editor()
  44. {
  45. m_documentation_tooltip_window = GUI::Window::construct();
  46. m_documentation_tooltip_window->set_rect(0, 0, 500, 400);
  47. m_documentation_tooltip_window->set_window_type(GUI::WindowType::Tooltip);
  48. m_documentation_html_view = m_documentation_tooltip_window->set_main_widget<HtmlView>();
  49. }
  50. Editor::~Editor()
  51. {
  52. }
  53. EditorWrapper& Editor::wrapper()
  54. {
  55. return static_cast<EditorWrapper&>(*parent());
  56. }
  57. const EditorWrapper& Editor::wrapper() const
  58. {
  59. return static_cast<const EditorWrapper&>(*parent());
  60. }
  61. void Editor::focusin_event(Core::Event& event)
  62. {
  63. wrapper().set_editor_has_focus({}, true);
  64. if (on_focus)
  65. on_focus();
  66. GUI::TextEditor::focusin_event(event);
  67. }
  68. void Editor::focusout_event(Core::Event& event)
  69. {
  70. wrapper().set_editor_has_focus({}, false);
  71. GUI::TextEditor::focusout_event(event);
  72. }
  73. void Editor::paint_event(GUI::PaintEvent& event)
  74. {
  75. GUI::TextEditor::paint_event(event);
  76. if (is_focused()) {
  77. GUI::Painter painter(*this);
  78. painter.add_clip_rect(event.rect());
  79. auto rect = frame_inner_rect();
  80. if (vertical_scrollbar().is_visible())
  81. rect.set_width(rect.width() - vertical_scrollbar().width());
  82. if (horizontal_scrollbar().is_visible())
  83. rect.set_height(rect.height() - horizontal_scrollbar().height());
  84. painter.draw_rect(rect, palette().selection());
  85. }
  86. }
  87. static HashMap<String, String>& man_paths()
  88. {
  89. static HashMap<String, String> paths;
  90. if (paths.is_empty()) {
  91. // FIXME: This should also search man3, possibly other places..
  92. Core::DirIterator it("/usr/share/man/man2", Core::DirIterator::Flags::SkipDots);
  93. while (it.has_next()) {
  94. auto path = String::format("/usr/share/man/man2/%s", it.next_path().characters());
  95. auto title = FileSystemPath(path).title();
  96. paths.set(title, path);
  97. }
  98. }
  99. return paths;
  100. }
  101. void Editor::show_documentation_tooltip_if_available(const String& hovered_token, const Gfx::Point& screen_location)
  102. {
  103. auto it = man_paths().find(hovered_token);
  104. if (it == man_paths().end()) {
  105. #ifdef EDITOR_DEBUG
  106. dbg() << "no man path for " << hovered_token;
  107. #endif
  108. m_documentation_tooltip_window->hide();
  109. return;
  110. }
  111. if (m_documentation_tooltip_window->is_visible() && hovered_token == m_last_parsed_token) {
  112. return;
  113. }
  114. #ifdef EDITOR_DEBUG
  115. dbg() << "opening " << it->value;
  116. #endif
  117. auto file = Core::File::construct(it->value);
  118. if (!file->open(Core::File::ReadOnly)) {
  119. dbg() << "failed to open " << it->value << " " << file->error_string();
  120. return;
  121. }
  122. MDDocument man_document;
  123. bool success = man_document.parse(file->read_all());
  124. if (!success) {
  125. dbg() << "failed to parse markdown";
  126. return;
  127. }
  128. auto html_text = man_document.render_to_html();
  129. auto html_document = parse_html_document(html_text);
  130. if (!html_document) {
  131. dbg() << "failed to parse HTML";
  132. return;
  133. }
  134. // FIXME: LibHTML needs a friendlier DOM manipulation API. Something like innerHTML :^)
  135. auto style_element = create_element(*html_document, "style");
  136. style_element->append_child(adopt(*new Text(*html_document, "body { background-color: #dac7b5; }")));
  137. // FIXME: This const_cast should not be necessary.
  138. auto* head_element = const_cast<HTMLHeadElement*>(html_document->head());
  139. ASSERT(head_element);
  140. head_element->append_child(style_element);
  141. m_documentation_html_view->set_document(html_document);
  142. m_documentation_tooltip_window->move_to(screen_location.translated(4, 4));
  143. m_documentation_tooltip_window->show();
  144. m_last_parsed_token = hovered_token;
  145. }
  146. void Editor::mousemove_event(GUI::MouseEvent& event)
  147. {
  148. GUI::TextEditor::mousemove_event(event);
  149. if (document().spans().is_empty())
  150. return;
  151. auto text_position = text_position_at(event.position());
  152. if (!text_position.is_valid()) {
  153. GUI::Application::the().hide_tooltip();
  154. return;
  155. }
  156. for (auto& span : document().spans()) {
  157. if (span.range.contains(text_position)) {
  158. auto adjusted_range = span.range;
  159. adjusted_range.end().set_column(adjusted_range.end().column() + 1);
  160. auto hovered_span_text = document().text_in_range(adjusted_range);
  161. #ifdef EDITOR_DEBUG
  162. dbg() << "Hovering: " << adjusted_range << " \"" << hovered_span_text << "\"";
  163. #endif
  164. show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location()));
  165. return;
  166. }
  167. }
  168. GUI::Application::the().hide_tooltip();
  169. }