TerminalWidget.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2018-2021, 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/Range.h>
  36. #include <LibVT/Terminal.h>
  37. class TerminalWidget final
  38. : public GUI::Frame
  39. , public VT::TerminalClient {
  40. C_OBJECT(TerminalWidget);
  41. public:
  42. TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtr<Core::ConfigFile> config);
  43. virtual ~TerminalWidget() override;
  44. void set_pty_master_fd(int fd);
  45. void inject_string(const StringView& string)
  46. {
  47. m_terminal.inject_string(string);
  48. flush_dirty_lines();
  49. }
  50. void flush_dirty_lines();
  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. enum class BellMode {
  56. Visible,
  57. AudibleBeep,
  58. Disabled
  59. };
  60. BellMode bell_mode() { return m_bell_mode; }
  61. void set_bell_mode(BellMode bm) { m_bell_mode = bm; };
  62. RefPtr<Core::ConfigFile> config() const { return m_config; }
  63. bool has_selection() const;
  64. bool selection_contains(const VT::Position&) const;
  65. String selected_text() const;
  66. VT::Range normalized_selection() const { return m_selection.normalized(); }
  67. void set_selection(const VT::Range& selection);
  68. VT::Position buffer_position_at(const Gfx::IntPoint&) const;
  69. VT::Range find_next(const StringView&, const VT::Position& start = {}, bool case_sensitivity = false, bool should_wrap = false);
  70. VT::Range find_previous(const StringView&, const VT::Position& start = {}, bool case_sensitivity = false, bool should_wrap = false);
  71. void scroll_to_bottom();
  72. void scroll_to_row(int);
  73. bool is_scrollable() const;
  74. int scroll_length() const;
  75. size_t max_history_size() const { return m_terminal.max_history_size(); }
  76. void set_max_history_size(size_t value) { m_terminal.set_max_history_size(value); }
  77. GUI::Action& copy_action() { return *m_copy_action; }
  78. GUI::Action& paste_action() { return *m_paste_action; }
  79. GUI::Action& clear_including_history_action() { return *m_clear_including_history_action; }
  80. void copy();
  81. void paste();
  82. void clear_including_history();
  83. Function<void(const StringView&)> on_title_change;
  84. Function<void(const Gfx::IntSize&)> on_terminal_size_change;
  85. Function<void()> on_command_exit;
  86. GUI::Menu& context_menu() { return *m_context_menu; }
  87. void set_font_and_resize_to_fit(const Gfx::Font&);
  88. private:
  89. // ^GUI::Widget
  90. virtual void event(Core::Event&) override;
  91. virtual void paint_event(GUI::PaintEvent&) override;
  92. virtual void resize_event(GUI::ResizeEvent&) override;
  93. virtual void keydown_event(GUI::KeyEvent&) override;
  94. virtual void keyup_event(GUI::KeyEvent&) override;
  95. virtual void mousedown_event(GUI::MouseEvent&) override;
  96. virtual void mouseup_event(GUI::MouseEvent&) override;
  97. virtual void mousemove_event(GUI::MouseEvent&) override;
  98. virtual void mousewheel_event(GUI::MouseEvent&) override;
  99. virtual void doubleclick_event(GUI::MouseEvent&) override;
  100. virtual void focusin_event(GUI::FocusEvent&) override;
  101. virtual void focusout_event(GUI::FocusEvent&) override;
  102. virtual void context_menu_event(GUI::ContextMenuEvent&) override;
  103. virtual void drop_event(GUI::DropEvent&) override;
  104. virtual void leave_event(Core::Event&) override;
  105. virtual void did_change_font() override;
  106. // ^TerminalClient
  107. virtual void beep() override;
  108. virtual void set_window_title(const StringView&) override;
  109. virtual void set_window_progress(int value, int max) override;
  110. virtual void terminal_did_resize(u16 columns, u16 rows) override;
  111. virtual void terminal_history_changed() override;
  112. virtual void emit(const u8*, size_t) override;
  113. void set_logical_focus(bool);
  114. Gfx::IntRect glyph_rect(u16 row, u16 column);
  115. Gfx::IntRect row_rect(u16 row);
  116. Gfx::IntSize widget_size_for_font(const Gfx::Font&) const;
  117. void update_cursor();
  118. void invalidate_cursor();
  119. void relayout(const Gfx::IntSize&);
  120. void update_copy_action();
  121. void update_paste_action();
  122. Gfx::IntSize compute_base_size() const;
  123. int first_selection_column_on_row(int row) const;
  124. int last_selection_column_on_row(int row) const;
  125. u32 code_point_at(const VT::Position&) const;
  126. VT::Position next_position_after(const VT::Position&, bool should_wrap) const;
  127. VT::Position previous_position_before(const VT::Position&, bool should_wrap) const;
  128. VT::Terminal m_terminal;
  129. VT::Range m_selection;
  130. String m_hovered_href;
  131. String m_hovered_href_id;
  132. String m_active_href;
  133. String m_active_href_id;
  134. // Snapshot of m_hovered_href when opening a context menu for a hyperlink.
  135. String m_context_menu_href;
  136. BellMode m_bell_mode { BellMode::Visible };
  137. bool m_alt_key_held { false };
  138. bool m_rectangle_selection { false };
  139. int m_pixel_width { 0 };
  140. int m_pixel_height { 0 };
  141. int m_inset { 2 };
  142. int m_line_spacing { 4 };
  143. int m_line_height { 0 };
  144. int m_ptm_fd { -1 };
  145. bool m_has_logical_focus { false };
  146. bool m_in_relayout { false };
  147. RefPtr<Core::Notifier> m_notifier;
  148. u8 m_opacity { 255 };
  149. bool m_cursor_blink_state { true };
  150. bool m_automatic_size_policy { false };
  151. RefPtr<Gfx::Font> m_bold_font;
  152. enum class AutoScrollDirection {
  153. None,
  154. Up,
  155. Down
  156. };
  157. AutoScrollDirection m_auto_scroll_direction { AutoScrollDirection::None };
  158. RefPtr<Core::Timer> m_cursor_blink_timer;
  159. RefPtr<Core::Timer> m_visual_beep_timer;
  160. RefPtr<Core::Timer> m_auto_scroll_timer;
  161. RefPtr<Core::ConfigFile> m_config;
  162. RefPtr<GUI::ScrollBar> m_scrollbar;
  163. RefPtr<GUI::Action> m_copy_action;
  164. RefPtr<GUI::Action> m_paste_action;
  165. RefPtr<GUI::Action> m_clear_including_history_action;
  166. RefPtr<GUI::Menu> m_context_menu;
  167. RefPtr<GUI::Menu> m_context_menu_for_hyperlink;
  168. Core::ElapsedTimer m_triple_click_timer;
  169. Gfx::IntPoint m_left_mousedown_position;
  170. };