mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-25 22:08:59 +00:00
GApplication now has a palette. This palette contains all the system theme colors by default, and is inherited by a new top-level GWidget. New child widgets inherit their parents palette. It is possible to override the GApplication palette, and the palette of any GWidget. The Palette object contains a bunch of colors, each corresponding to a ColorRole. Each role has a convenience getter as well. Each GWidget now has a background_role() and foreground_role(), which are then looked up in their current palette when painting. This means that you no longer alter the background color of a widget by setting it directly, rather you alter either its background role, or the widget's palette.
117 lines
4.9 KiB
C++
117 lines
4.9 KiB
C++
#include <AK/StringBuilder.h>
|
|
#include <Kernel/KeyCode.h>
|
|
#include <LibDraw/Palette.h>
|
|
#include <LibGUI/GAction.h>
|
|
#include <LibGUI/GMenu.h>
|
|
#include <LibGUI/GModel.h>
|
|
#include <LibGUI/GPainter.h>
|
|
#include <LibGUI/GScrollBar.h>
|
|
#include <LibGUI/GTableView.h>
|
|
#include <LibGUI/GTextBox.h>
|
|
#include <LibGUI/GWindow.h>
|
|
|
|
GTableView::GTableView(GWidget* parent)
|
|
: GAbstractColumnView(parent)
|
|
{
|
|
set_background_role(ColorRole::Base);
|
|
}
|
|
|
|
GTableView::~GTableView()
|
|
{
|
|
}
|
|
|
|
void GTableView::paint_event(GPaintEvent& event)
|
|
{
|
|
Color widget_background_color = palette().color(background_role());
|
|
GFrame::paint_event(event);
|
|
|
|
GPainter painter(*this);
|
|
painter.add_clip_rect(frame_inner_rect());
|
|
painter.add_clip_rect(event.rect());
|
|
painter.fill_rect(event.rect(), widget_background_color);
|
|
painter.translate(frame_thickness(), frame_thickness());
|
|
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
|
|
|
if (!model())
|
|
return;
|
|
|
|
int exposed_width = max(content_size().width(), width());
|
|
int y_offset = header_height();
|
|
|
|
bool dummy;
|
|
int first_visible_row = index_at_event_position(frame_inner_rect().top_left(), dummy).row();
|
|
int last_visible_row = index_at_event_position(frame_inner_rect().bottom_right(), dummy).row();
|
|
|
|
if (first_visible_row == -1)
|
|
first_visible_row = 0;
|
|
if (last_visible_row == -1)
|
|
last_visible_row = model()->row_count() - 1;
|
|
|
|
int painted_item_index = first_visible_row;
|
|
|
|
for (int row_index = first_visible_row; row_index <= last_visible_row; ++row_index) {
|
|
bool is_selected_row = selection().contains_row(row_index);
|
|
int y = y_offset + painted_item_index * item_height();
|
|
|
|
Color background_color;
|
|
Color key_column_background_color;
|
|
if (is_selected_row) {
|
|
background_color = is_focused() ? palette().selection() : Color::from_rgb(0x606060);
|
|
key_column_background_color = is_focused() ? palette().selection() : Color::from_rgb(0x606060);
|
|
} else {
|
|
if (alternating_row_colors() && (painted_item_index % 2)) {
|
|
background_color = widget_background_color.darkened(0.8f);
|
|
key_column_background_color = widget_background_color.darkened(0.7f);
|
|
} else {
|
|
background_color = widget_background_color;
|
|
key_column_background_color = widget_background_color.darkened(0.9f);
|
|
}
|
|
}
|
|
painter.fill_rect(row_rect(painted_item_index), background_color);
|
|
|
|
int x_offset = 0;
|
|
for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
|
|
if (is_column_hidden(column_index))
|
|
continue;
|
|
auto column_metadata = model()->column_metadata(column_index);
|
|
int column_width = this->column_width(column_index);
|
|
const Font& font = column_metadata.font ? *column_metadata.font : this->font();
|
|
bool is_key_column = model()->key_column() == column_index;
|
|
Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
|
|
if (is_key_column) {
|
|
auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
|
|
painter.fill_rect(cell_rect_for_fill, key_column_background_color);
|
|
}
|
|
auto cell_index = model()->index(row_index, column_index);
|
|
|
|
if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) {
|
|
delegate->paint(painter, cell_rect, palette(), *model(), cell_index);
|
|
} else {
|
|
auto data = model()->data(cell_index);
|
|
if (data.is_bitmap()) {
|
|
painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
|
|
} else if (data.is_icon()) {
|
|
if (auto bitmap = data.as_icon().bitmap_for_size(16))
|
|
painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
|
|
} else {
|
|
Color text_color;
|
|
if (is_selected_row)
|
|
text_color = palette().selection_text();
|
|
else
|
|
text_color = model()->data(cell_index, GModel::Role::ForegroundColor).to_color(palette().color(foreground_role()));
|
|
painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color, TextElision::Right);
|
|
}
|
|
}
|
|
x_offset += column_width + horizontal_padding() * 2;
|
|
}
|
|
++painted_item_index;
|
|
};
|
|
|
|
Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
|
|
painter.fill_rect(unpainted_rect, widget_background_color);
|
|
|
|
// Untranslate the painter vertically and do the column headers.
|
|
painter.translate(0, vertical_scrollbar().value());
|
|
if (headers_visible())
|
|
paint_headers(painter);
|
|
}
|