Files
lightgui/widgets/components/GtkLayout/GtkLayout.h

405 lines
9.0 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region GtkLayout
class GtkLayout
{
public:
struct Rect
{
int x1;
int y1;
int x2;
int y2;
};
private:
Display *display_;
int screen_;
Window window_;
GC gc_;
SStyle_Manager *styles_;
int x1_, y1_, x2_, y2_;
std::string name_;
std::string text_;
std::string style_name_; // "" = manager's active style
bool visible_ = true;
bool enabled_ = true;
bool focused_ = false;
int virtual_width_ = 2000;
int virtual_height_ = 1500;
int hadjust_ = 0; // horizontal scroll offset into the virtual area
int vadjust_ = 0; // vertical scroll offset into the virtual area
int grid_step_ = 32; // dot spacing in virtual coordinates
int scroll_step_ = 32; // one wheel notch / arrow key
// Colours exposed by the getter/setter pass; defaults are the old hardcoded values.
Srgb canvas_color_ = {250, 250, 250};
Srgb grid_color_ = {170, 175, 185};
Srgb scrollbar_track_color_ = {225, 225, 228};
Srgb scrollbar_thumb_color_ = {150, 155, 165};
public:
GtkLayout(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height)
{
display_ = display;
screen_ = screen;
window_ = window;
gc_ = gc;
styles_ = styles;
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
name_ = "GtkLayout";
text_ = "Layout";
}
~GtkLayout() {}
void set_bounds(int x, int y, int width, int height)
{
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
scroll_to(hadjust_, vadjust_); // re-clamp against the new size
}
int width() { return x2_ - x1_; }
int height() { return y2_ - y1_; }
bool contains(int x, int y) { return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; }
void set_visible(bool v) { visible_ = v; }
bool get_visible() { return visible_; }
void set_enabled(bool e) { enabled_ = e; }
bool get_enabled() { return enabled_; }
void set_style_name(std::string s) { style_name_ = s; }
void set_text(std::string t) { text_ = t; }
std::string get_text() { return text_; }
Srgb get_canvas_color()
{
return canvas_color_;
}
void set_canvas_color(Srgb c)
{
canvas_color_ = c;
}
void set_canvas_color_rgb(int r, int g, int b)
{
canvas_color_ = Srgb{r, g, b};
}
Srgb get_grid_color()
{
return grid_color_;
}
void set_grid_color(Srgb c)
{
grid_color_ = c;
}
void set_grid_color_rgb(int r, int g, int b)
{
grid_color_ = Srgb{r, g, b};
}
Srgb get_scrollbar_track_color()
{
return scrollbar_track_color_;
}
void set_scrollbar_track_color(Srgb c)
{
scrollbar_track_color_ = c;
}
void set_scrollbar_track_color_rgb(int r, int g, int b)
{
scrollbar_track_color_ = Srgb{r, g, b};
}
Srgb get_scrollbar_thumb_color()
{
return scrollbar_thumb_color_;
}
void set_scrollbar_thumb_color(Srgb c)
{
scrollbar_thumb_color_ = c;
}
void set_scrollbar_thumb_color_rgb(int r, int g, int b)
{
scrollbar_thumb_color_ = Srgb{r, g, b};
}
int get_grid_step()
{
return grid_step_;
}
void set_grid_step(int v)
{
grid_step_ = v;
}
int get_scroll_step()
{
return scroll_step_;
}
void set_scroll_step(int v)
{
scroll_step_ = v;
}
SStyle style() { return styles_->resolve(style_name_); }
void set_virtual_size(int w, int h)
{
virtual_width_ = std::max(1, w);
virtual_height_ = std::max(1, h);
scroll_to(hadjust_, vadjust_);
}
int get_virtual_width() { return virtual_width_; }
int get_virtual_height() { return virtual_height_; }
// Offsets are clamped so the view never scrolls past the end of
// the virtual area (or below zero).
void scroll_to(int x, int y)
{
hadjust_ = std::max(0, std::min(x, std::max(0, virtual_width_ - width())));
vadjust_ = std::max(0, std::min(y, std::max(0, virtual_height_ - height())));
}
void scroll_by(int dx, int dy) { scroll_to(hadjust_ + dx, vadjust_ + dy); }
int get_hadjust() { return hadjust_; }
int get_vadjust() { return vadjust_; }
// The visible window, in virtual-area coordinates.
Rect visible_rect() { return Rect { hadjust_, vadjust_, hadjust_ + width(), vadjust_ + height() }; }
void draw()
{
if (!visible_)
{
return;
}
SStyle st = style();
SStyle frame = st;
frame.fillColor_ = styles_->get_color_Srgb(canvas_color_);
xf::draw_polished(display_, window_, gc_, frame, x1_, y1_, x2_, y2_);
// Grid dots across the visible window, shifted by the scroll
// offsets so the pattern stays fixed to the virtual area.
XSetForeground(display_, gc_, styles_->get_color_Srgb(grid_color_));
int first_vx = (hadjust_ / grid_step_) * grid_step_;
int first_vy = (vadjust_ / grid_step_) * grid_step_;
for (int vy = first_vy; vy < vadjust_ + height(); vy += grid_step_)
{
int sy = y1_ + (vy - vadjust_);
if (sy < y1_ + 2 || sy > y2_ - 2)
{
continue;
}
for (int vx = first_vx; vx < hadjust_ + width(); vx += grid_step_)
{
int sx = x1_ + (vx - hadjust_);
if (sx >= x1_ + 2 && sx <= x2_ - 2)
{
XFillRectangle(display_, window_, gc_, sx - 1, sy - 1, 2, 2);
}
}
}
// Offset readout in the top-left corner.
std::string info = "Offset " + std::to_string(hadjust_) + ", " + std::to_string(vadjust_) + " of " + std::to_string(virtual_width_) + "x" + std::to_string(virtual_height_);
if (st.font_)
{
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 8 + st.font_->ascent, info);
}
draw_scrollbars();
XFlush(display_);
}
// Thin proportional indicators along the right and bottom edges.
void draw_scrollbars()
{
unsigned long track = styles_->get_color_Srgb(scrollbar_track_color_);
unsigned long thumb = styles_->get_color_Srgb(scrollbar_thumb_color_);
int bar = 8;
XSetForeground(display_, gc_, track);
XFillRectangle(display_, window_, gc_, x2_ - bar - 2, y1_ + 2, bar, height() - 4);
int thumb_h = std::max(16, height() * height() / std::max(1, virtual_height_));
int thumb_y = y1_ + 2 + (height() - 4 - thumb_h) * vadjust_ / std::max(1, std::max(1, virtual_height_ - height()));
XSetForeground(display_, gc_, thumb);
XFillRectangle(display_, window_, gc_, x2_ - bar - 2, thumb_y, bar, thumb_h);
XSetForeground(display_, gc_, track);
XFillRectangle(display_, window_, gc_, x1_ + 2, y2_ - bar - 2, width() - 4, bar);
int thumb_w = std::max(16, width() * width() / std::max(1, virtual_width_));
int thumb_x = x1_ + 2 + (width() - 4 - thumb_w) * hadjust_ / std::max(1, std::max(1, virtual_width_ - width()));
XSetForeground(display_, gc_, thumb);
XFillRectangle(display_, window_, gc_, thumb_x, y2_ - bar - 2, thumb_w, bar);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!contains(x, y) || !enabled_)
{
return false;
}
focused_ = true;
// X11 wheel: 4 = up, 5 = down, 6 = left, 7 = right.
if (button == 4) { scroll_by(0, -scroll_step_); return true; }
if (button == 5) { scroll_by(0, scroll_step_); return true; }
if (button == 6) { scroll_by(-scroll_step_, 0); return true; }
if (button == 7) { scroll_by(scroll_step_, 0); return true; }
return button == 1;
}
bool on_button_release(int x, int y, unsigned int button)
{
(void)x;
(void)y;
(void)button;
return false;
}
bool on_motion(int x, int y)
{
(void)x;
(void)y;
return false;
}
bool on_key(KeySym key, const char* str)
{
(void)str;
if (!focused_ || !enabled_)
{
return false;
}
if (key == XK_Left) { scroll_by(-scroll_step_, 0); return true; }
if (key == XK_Right) { scroll_by(scroll_step_, 0); return true; }
if (key == XK_Up) { scroll_by(0, -scroll_step_); return true; }
if (key == XK_Down) { scroll_by(0, scroll_step_); return true; }
if (key == XK_Page_Down) { scroll_by(0, height()); return true; }
if (key == XK_Page_Up) { scroll_by(0, -height()); return true; }
if (key == XK_Home) { scroll_to(0, 0); return true; }
if (key == XK_End) { scroll_to(virtual_width_, virtual_height_); return true; }
return false;
}
};
//endregion