gtk_renderer.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "lumacs/editor_core.hpp"
  3. #include "lumacs/window.hpp"
  4. #include "lumacs/theme.hpp"
  5. #include <gtkmm.h>
  6. #include <pangomm.h>
  7. #include <memory>
  8. #include <optional>
  9. namespace lumacs {
  10. // Forward declarations
  11. class EditorCore;
  12. class Window;
  13. // Constants for padding
  14. static constexpr double PADDING_LEFT = 8.0;
  15. static constexpr double PADDING_TOP = 8.0;
  16. static constexpr double PADDING_RIGHT = 8.0;
  17. static constexpr double PADDING_BOTTOM = 8.0;
  18. class GtkRenderer {
  19. public:
  20. GtkRenderer(EditorCore& core, Gtk::DrawingArea& main_drawing_area);
  21. void initialize_font_metrics();
  22. void apply_face_attributes(Pango::AttrList& attr_list, const FaceAttributes& face, int start_index, int end_index);
  23. std::optional<Position> resolve_screen_pos(std::shared_ptr<Window> window, double x, double y);
  24. void on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height,
  25. std::shared_ptr<Window> active_window_cache, bool cursor_visible_state);
  26. void draw_window(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height,
  27. std::shared_ptr<Window> window, Gtk::DrawingArea* widget,
  28. std::shared_ptr<Window> active_window_cache, bool cursor_visible_state);
  29. void render_modeline_for_window(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height,
  30. std::shared_ptr<Window> window,
  31. std::shared_ptr<Window> active_window_cache);
  32. void render_minibuffer(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height);
  33. /// @brief Gets the screen coordinates and dimensions of the minibuffer area.
  34. /// @param x Output parameter for the X coordinate.
  35. /// @param y Output parameter for the Y coordinate.
  36. /// @param width Output parameter for the width.
  37. /// @param height Output parameter for the height.
  38. /// @return True if the coordinates could be determined, false otherwise.
  39. bool get_minibuffer_coords(int& x, int& y, int& width, int& height) const;
  40. /// @brief Clears the internal render cache, forcing a full redraw.
  41. /// Useful when the theme changes.
  42. void invalidate_cache();
  43. private:
  44. struct LineCacheEntry {
  45. std::string text_content;
  46. std::vector<StyledRange> styles; // Check if styles changed
  47. Cairo::RefPtr<Cairo::ImageSurface> surface;
  48. int width = 0;
  49. int height = 0;
  50. };
  51. // Map Window ID (or ptr) -> (Line Index -> Cache Entry)
  52. std::unordered_map<Window*, std::unordered_map<size_t, LineCacheEntry>> render_cache_;
  53. EditorCore& core_;
  54. Gtk::DrawingArea& main_drawing_area_;
  55. Pango::FontDescription font_desc_;
  56. bool font_initialized_ = false;
  57. double char_width_ = 0;
  58. double line_height_ = 0;
  59. double ascent_ = 0;
  60. };
  61. } // namespace lumacs