summaryrefslogtreecommitdiffstats
path: root/src/ui/grid/context.rs
blob: d5abef9f11c2d371286c904a89b3ed3e7178b0b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use cairo;
use gtk::DrawingArea;
use pango;

use gdk::prelude::*;
use gtk::prelude::*;

use log::warn;

use crate::ui::color::{Color, Highlight, HlDefs};
use crate::ui::font::Font;
use crate::ui::grid::render;
use crate::ui::grid::row::Row;

/// Context is manipulated by Grid.
pub struct Context {
    /// Our cairo context, that is evetually drawn to the screen.
    pub cairo_context: cairo::Context,
    /// Our cell metrics.
    pub cell_metrics: CellMetrics,
    /// Cell metrics to be updated.
    pub cell_metrics_update: Option<CellMetrics>,

    /// Internal grid.
    pub rows: Vec<Row>,

    /// Cursor, (row, col):
    pub cursor: (u64, u64),
    /// Cursor alpha color. Used to make the cursor blink.
    pub cursor_alpha: f64,
    /// The duration of the cursor blink
    pub cursor_blink_on: u64,
    /// Width of the cursor.
    pub cursor_cell_percentage: f64,
    /// Color of the cursor.
    pub cursor_color: Color,
    /// If the current status is busy or not. When busy, the cursor is not
    /// drawn (like when in terminal mode in inserting text).
    pub busy: bool,
    /// Cairo context for cursor.
    pub cursor_context: cairo::Context,

    /// Current highlight.
    pub current_hl: Highlight,
    /// If the grid that this context belongs to is active or not.
    pub active: bool,

    /// Areas to call queue_draw_area on the drawing area on flush.
    pub queue_draw_area: Vec<(f64, f64, f64, f64)>,
}

impl Context {
    pub fn new(
        da: &DrawingArea,
        win: &gdk::Window,
        font: Font,
        line_space: i64,
        cols: usize,
        rows: usize,
        hl_defs: &HlDefs,
    ) -> Self {
        let pango_context = da.get_pango_context().unwrap();

        let font_desc = font.as_pango_font();
        pango_context.set_font_description(&font_desc);

        let mut cell_metrics = CellMetrics::default();
        cell_metrics.font = font;
        cell_metrics.line_space = line_space;
        cell_metrics.update(&pango_context);

        let w = cell_metrics.width as usize * cols;
        let h = cell_metrics.height as usize * rows;
        let surface = create_surface_clamp(win, w as i32, h as i32);

        let cairo_context = cairo::Context::new(&surface);

        // Fill the context with default bg color.
        cairo_context.save();
        cairo_context.set_source_rgb(
            hl_defs.default_bg.r,
            hl_defs.default_bg.g,
            hl_defs.default_bg.b,
        );
        cairo_context.paint();
        cairo_context.restore();

        let cursor_context = {
            let surface = win
                .create_similar_surface(
                    cairo::Content::ColorAlpha,
                    (cell_metrics.width * 2.0) as i32, // times two for double width chars.
                    cell_metrics.height as i32 + cell_metrics.ascent as i32,
                )
                .unwrap();
            cairo::Context::new(&surface)
        };

        Context {
            cairo_context,
            cell_metrics,
            cell_metrics_update: None,
            rows: vec![],

            cursor: (0, 0),
            cursor_alpha: 1.0,
            cursor_blink_on: 0,
            cursor_cell_percentage: 1.0,
            cursor_color: Color::from_u64(0),
            busy: false,
            cursor_context,

            current_hl: Highlight::default(),
            active: false,

            queue_draw_area: vec![],
        }
    }

    /// Updates internals that are dependant on the drawing area.
    pub fn update(
        &mut self,
        da: &DrawingArea,
        win: &gdk::Window,
        cols: usize,
        rows: usize,
        hl_defs: &HlDefs,
        prev_cols: usize,
        prev_rows: usize,
    ) {
        let pctx = da.get_pango_context().unwrap();
        pctx.set_font_description(&self.cell_metrics.font.as_pango_font());

        self.cell_metrics.update(&pctx);

        let w = self.cell_metrics.width as usize * cols;
        let h = self.cell_metrics.height as usize * rows;
        let surface = create_surface_clamp(win, w as i32, h as i32);
        let ctx = cairo::Context::new(&surface);

        // Fill the context with default bg color.
        ctx.save();
        ctx.set_source_rgb(
            hl_defs.default_bg.r,
            hl_defs.default_bg.g,
            hl_defs.default_bg.b,
        );
        ctx.paint();
        ctx.restore();

        let s = self.cairo_context.get_target();
        self.cairo_context.save();
        ctx.set_source_surface(&s, 0.0, 0.0);
        ctx.set_operator(cairo::Operator::Source);
        // Make sure we only paint the area that _was_ visible before this update
        // so we don't undo the bg color paint we did earlier.
        ctx.rectangle(
            0.0,
            0.0,
            self.cell_metrics.width * prev_cols as f64,
            self.cell_metrics.height * prev_rows as f64,
        );
        ctx.fill();
        self.cairo_context.restore();

        self.cairo_context = ctx;
    }

    /// Sets the cell metrics to be updated. If font or line_space is None,
    /// the earlier value for each is used. Call `finish_metrics_update` to
    /// make the update take place.
    pub fn update_metrics(
        &mut self,
        font: Font,
        line_space: i64,
        da: &gtk::DrawingArea,
        win: &gdk::Window,
    ) {
        let pango_context = da.get_pango_context().unwrap();
        pango_context.set_font_description(&font.as_pango_font());

        self.cell_metrics.font = font;
        self.cell_metrics.line_space = line_space;
        self.cell_metrics.update(&pango_context);

        self.cursor_context = {
            let surface = win
                .create_similar_surface(
                    cairo::Content::ColorAlpha,
                    (self.cell_metrics.width * 2.0) as i32, // times two for double width chars.
                    self.cell_metrics.height as i32
                        + self.cell_metrics.ascent as i32,
                )
                .unwrap();
            cairo::Context::new(&surface)
        };
    }

    /// Returns x, y, width and height for current cursor location.
    pub fn get_cursor_rect(&self) -> (i32, i32, i32, i32) {
        let double_width = self
            .rows
            .get(self.cursor.0 as usize)
            .and_then(|row| {
                Some(row.cell_at(self.cursor.1 as usize).double_width)
            })
            .unwrap_or(false);

        let cm = &self.cell_metrics;
        let (x, y) = render::get_coords(
            cm.height,
            cm.width,
            self.cursor.0 as f64,
            self.cursor.1 as f64,
        );
        (
            x.floor() as i32,
            y.floor() as i32,
            if double_width {
                (cm.width * 2.0).ceil() as i32
            } else {
                cm.width.ceil() as i32
            },
            cm.height.ceil() as i32,
        )
    }
}

/// Cell metrics tells the size (and other metrics) of the cells in a grid.
#[derive(Default, Debug, Clone)]
pub struct CellMetrics {
    pub height: f64,
    pub width: f64,
    pub ascent: f64,
    pub decent: f64,
    pub underline_thickness: f64,
    pub underline_position: f64,

    pub line_space: i64,
    pub font: Font,
}

impl CellMetrics {
    pub fn update(&mut self, ctx: &pango::Context) {
        let fm = ctx
            .get_metrics(Some(&self.font.as_pango_font()), None)
            .unwrap();
        let extra = self.line_space as f64 / 2.0;
        let scale = f64::from(pango::SCALE);
        self.ascent = (f64::from(fm.get_ascent()) / scale + extra).ceil();
        self.decent = (f64::from(fm.get_descent()) / scale + extra).ceil();
        self.height = self.ascent + self.decent;
        self.width = f64::from(fm.get_approximate_char_width()) / scale;

        self.underline_position =
            f64::from(fm.get_underline_position()) / scale - extra;
        // TODO(ville): make the underline thickness a bit thicker (one 10th of the cell height?).
        self.underline_thickness =
            f64::from(fm.get_underline_thickness()) / scale * 2.0;
    }
}

/// Creates a new surface from `win`. Clamps width and height to the size of `win`.
/// When clamping, warn level log messages are emitted.
fn create_surface_clamp(win: &gdk::Window, w: i32, h: i32) -> cairo::Surface {
    let max_height = win.get_height();
    let max_wid