TerminalWidget.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #pragma once
  27. #include <AK/String.h>
  28. #include <LibCore/ConfigFile.h>
  29. #include <LibCore/ElapsedTimer.h>
  30. #include <LibCore/Notifier.h>
  31. #include <LibCore/Timer.h>
  32. #include <LibGUI/Frame.h>
  33. #include <LibGfx/Bitmap.h>
  34. #include <LibGfx/Rect.h>
  35. #include <LibVT/Terminal.h>
  36. class TerminalWidget final : public GUI::Frame
  37. , public VT::TerminalClient {
  38. C_OBJECT(TerminalWidget)
  39. public:
  40. TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtr<Core::ConfigFile> config);
  41. virtual ~TerminalWidget() override;
  42. void set_pty_master_fd(int fd);
  43. void inject_string(const StringView& string)
  44. {
  45. m_terminal.inject_string(string);
  46. flush_dirty_lines();
  47. }
  48. void create_window();
  49. void flush_dirty_lines();
  50. void force_repaint();
  51. void apply_size_increments_to_window(GUI::Window&);
  52. const Gfx::Font& bold_font() const { return *m_bold_font; }
  53. void set_opacity(u8);
  54. float opacity() { return m_opacity; };
  55. bool should_beep() { return m_should_beep; }
  56. void set_should_beep(bool sb) { m_should_beep = sb; };
  57. RefPtr<Core::ConfigFile> config() const { return m_config; }
  58. bool has_selection() const;
  59. bool selection_contains(const VT::Position&) const;
  60. String selected_text() const;
  61. VT::Position buffer_position_at(const Gfx::Point&) const;
  62. VT::Position normalized_selection_start() const;
  63. VT::Position normalized_selection_end() const;
  64. bool is_scrollable() const;
  65. GUI::Action& copy_action() { return *m_copy_action; }
  66. GUI::Action& paste_action() { return *m_paste_action; }
  67. void copy();
  68. void paste();
  69. virtual bool accepts_focus() const override { return true; }
  70. Function<void(const StringView&)> on_title_change;
  71. Function<void()> on_command_exit;
  72. private:
  73. // ^GUI::Widget
  74. virtual void event(Core::Event&) override;
  75. virtual void paint_event(GUI::PaintEvent&) override;
  76. virtual void resize_event(GUI::ResizeEvent&) override;
  77. virtual void keydown_event(GUI::KeyEvent&) override;
  78. virtual void keyup_event(GUI::KeyEvent&) override;
  79. virtual void mousedown_event(GUI::MouseEvent&) override;
  80. virtual void mouseup_event(GUI::MouseEvent&) override;
  81. virtual void mousemove_event(GUI::MouseEvent&) override;
  82. virtual void mousewheel_event(GUI::MouseEvent&) override;
  83. virtual void doubleclick_event(GUI::MouseEvent&) override;
  84. virtual void focusin_event(Core::Event&) override;
  85. virtual void focusout_event(Core::Event&) override;
  86. virtual void context_menu_event(GUI::ContextMenuEvent&) override;
  87. virtual void drop_event(GUI::DropEvent&) override;
  88. virtual void leave_event(Core::Event&) override;
  89. virtual void did_change_font() override;
  90. // ^TerminalClient
  91. virtual void beep() override;
  92. virtual void set_window_title(const StringView&) override;
  93. virtual void terminal_did_resize(u16 columns, u16 rows) override;
  94. virtual void terminal_history_changed() override;
  95. virtual void emit(const u8*, size_t) override;
  96. void set_logical_focus(bool);
  97. Gfx::Rect glyph_rect(u16 row, u16 column);
  98. Gfx::Rect row_rect(u16 row);
  99. void update_cursor();
  100. void invalidate_cursor();
  101. void relayout(const Gfx::Size&);
  102. Gfx::Size compute_base_size() const;
  103. int first_selection_column_on_row(int row) const;
  104. int last_selection_column_on_row(int row) const;
  105. VT::Terminal m_terminal;
  106. VT::Position m_selection_start;
  107. VT::Position m_selection_end;
  108. String m_hovered_href;
  109. String m_hovered_href_id;
  110. String m_active_href;
  111. String m_active_href_id;
  112. // Snapshot of m_hovered_href when opening a context menu for a hyperlink.
  113. String m_context_menu_href;
  114. bool m_should_beep { false };
  115. bool m_belling { false };
  116. bool m_alt_key_held { false };
  117. bool m_rectangle_selection { false };
  118. int m_pixel_width { 0 };
  119. int m_pixel_height { 0 };
  120. int m_inset { 2 };
  121. int m_line_spacing { 4 };
  122. int m_line_height { 0 };
  123. int m_ptm_fd { -1 };
  124. bool m_has_logical_focus { false };
  125. RefPtr<Core::Notifier> m_notifier;
  126. u8 m_opacity { 255 };
  127. bool m_needs_background_fill { true };
  128. bool m_cursor_blink_state { true };
  129. bool m_automatic_size_policy { false };
  130. RefPtr<Gfx::Font> m_bold_font;
  131. int m_glyph_width { 0 };
  132. RefPtr<Core::Timer> m_cursor_blink_timer;
  133. RefPtr<Core::Timer> m_visual_beep_timer;
  134. RefPtr<Core::ConfigFile> m_config;
  135. RefPtr<GUI::ScrollBar> m_scrollbar;
  136. RefPtr<GUI::Action> m_copy_action;
  137. RefPtr<GUI::Action> m_paste_action;
  138. RefPtr<GUI::Menu> m_context_menu;
  139. RefPtr<GUI::Menu> m_context_menu_for_hyperlink;
  140. Core::ElapsedTimer m_triple_click_timer;
  141. Gfx::Point m_left_mousedown_position;
  142. };