jes/src/gui/BufferPane.cc

124 lines
3.8 KiB
C++

#include "BufferPane.h"
BufferPane::BufferPane(std::shared_ptr<Buffer> buffer,
std::shared_ptr<Font> font,
std::shared_ptr<GL> gl)
: m_buffer(buffer), m_font(font), m_gl(gl)
{
m_cursor_row = 0;
m_scroll_offset = 5;
m_cursor = buffer->add_cursor();
}
void BufferPane::resize(int width, int height)
{
Pane::resize(width, height);
m_columns = std::max(1, m_width / m_font->get_advance());
m_rows = std::max(1, (m_height + m_font->get_line_height() - 1) / m_font->get_line_height());
}
void BufferPane::update_cursor_row()
{
int so = effective_scroll_offset();
int rows_above = screen_rows_above_cursor(std::max(so, m_cursor_row));
int rows_below = screen_rows_below_cursor(std::max(so, m_rows - m_cursor_row - 1));
int min_rows_to_leave_above = std::min(rows_above, so);
int min_rows_to_leave_below = std::min(rows_below, so);
m_cursor_row = std::max(min_rows_to_leave_above, std::min(m_rows - min_rows_to_leave_below - 1, m_cursor_row));
}
int BufferPane::screen_rows_below_cursor(int stop_at)
{
Cursor cursor = *m_cursor;
size_t column = cursor.column();
cursor.go_end_of_line(false);
int rows = (cursor.column() / m_columns) - (column / m_columns);
while ((rows < stop_at) && cursor.go_down(0u))
{
cursor.go_end_of_line(false);
rows += (cursor.column() / m_columns) + 1;
}
return rows;
}
int BufferPane::screen_rows_above_cursor(int stop_at)
{
Cursor cursor = *m_cursor;
int rows = cursor.column() / m_columns;
while ((rows < stop_at) && cursor.go_up(0u))
{
Cursor cursor2 = cursor;
cursor2.go_end_of_line(false);
rows += (cursor2.column() / m_columns) + 1;
}
return rows;
}
void BufferPane::draw()
{
update_cursor_row();
int screen_row = m_cursor_row - m_cursor->column() / m_columns;
Cursor iter_cursor = *m_cursor;
iter_cursor.go_start_of_line();
while ((screen_row > 0) && iter_cursor.go_up(0u))
{
Cursor cursor2 = iter_cursor;
cursor2.go_end_of_line(false);
screen_row -= (1 + cursor2.column() / m_columns);
}
while (screen_row < m_rows)
{
draw_buffer_line(screen_row, iter_cursor);
iter_cursor.go_end_of_line(false);
screen_row += (1 + iter_cursor.column() / m_columns);
if (!iter_cursor.go_down(0u))
{
break;
}
}
}
void BufferPane::draw_buffer_line(int screen_row, const Cursor & cursor)
{
Cursor iter_cursor = cursor;
while (!iter_cursor.is_end_of_line(true))
{
int draw_row = screen_row + (iter_cursor.column() / m_columns);
if (draw_row < 0)
continue;
if (draw_row >= m_rows)
break;
int draw_column = iter_cursor.column() % m_columns;
int x, y;
colrow_to_xy(draw_column, draw_row, &x, &y);
uint32_t c = *iter_cursor;
if ((c != '\t') && (c != ' '))
{
m_gl->draw_character(win_x(x), win_y(y), c, *m_font);
}
iter_cursor.go_right(true);
}
}
void BufferPane::draw_buffer_character(int screen_column, int screen_row, uint32_t character)
{
int x, y;
colrow_to_xy(screen_column, screen_row, &x, &y);
m_gl->draw_character(win_x(x), win_y(y), character, *m_font);
}
void BufferPane::draw_cursor(int screen_column, int screen_row, bool insert_mode)
{
int x, y;
colrow_to_xy(screen_column, screen_row, &x, &y);
int width = insert_mode ? 1 : m_font->get_advance();
int height = m_font->get_line_height();
m_gl->draw_rect(win_x(x), win_y(y), width, height, 1.0, 0.2, 1.0, 1.0);
}
void BufferPane::colrow_to_xy(int col, int row, int * x, int * y)
{
*x = col * m_font->get_advance();
*y = m_height - (row + 1) * m_font->get_line_height();
}