Files

828 lines
11 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region GtkWindow
class GtkWindow
{
private:
Display *display_;
int screen_;
Window window_;
GC gc_;
SStyle_Manager *styles_;
int x1_, y1_, x2_, y2_;
std::string name_;
std::string text_; // the window title
std::string style_name_; // "" = manager's active style
bool visible_ = true;
bool enabled_ = true;
bool focused_ = false;
// Window-manager state, kept by the widget itself because in
// XFrame the "window" is drawn inside a host window.
bool closed_ = false;
bool iconified_ = false;
bool maximized_ = false;
bool resizable_ = true;
bool decorated_ = true;
// Phase 3 (§2.20): window-level Inspector props.
int border_style_ = 0; // 0 default, 1 none, 2 single, 3 dialog
std::string icon_;
int position_ = 0; // 0 default, 1 center, 2 screen center, 3 main-form center, 4 designed
int window_state_ = 0; // 0 normal, 1 minimized, 2 maximized, 3 fullscreen
bool fullscreen_ = false;
// Title-bar button the mouse is currently over: 0 none,
// 1 minimise, 2 maximise, 3 close.
int hover_button_ = 0;
// Bounds saved when maximising, restored on un-maximise.
int saved_x1_ = 0;
int saved_y1_ = 0;
int saved_x2_ = 0;
int saved_y2_ = 0;
int title_bar_height()
{
return 28;
}
// Title-bar button rectangles: three 26x20 buttons at the
// right of the title bar. which: 1 min, 2 max, 3 close.
void button_rect(int which, int &bx1, int &by1, int &bx2, int &by2)
{
by1 = y1_ + 4;
by2 = y1_ + title_bar_height() - 4;
bx2 = x2_ - 6 - (3 - which) * 30;
bx1 = bx2 - 26;
}
bool in_rect(int x, int y, int rx1, int ry1, int rx2, int ry2)
{
return x >= rx1 && x <= rx2 && y >= ry1 && y <= ry2;
}
// Colours exposed by the getter/setter pass; defaults are the old hardcoded values.
Srgb close_button_hover_color_ = {205, 35, 35};
public:
std::function<void()> on_close;
std::function<void()> on_minimize;
std::function<void()> on_maximize;
GtkWindow(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_ = "GtkWindow";
text_ = "Window";
}
~GtkWindow() {}
void set_bounds(int x, int y, int width, int height)
{
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
}
int width()
{
return x2_ - x1_;
}
int height()
{
return y2_ - y1_;
}
bool contains(int x, int y)
{
return visible_ && !closed_ && !iconified_ && 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_close_button_hover_color()
{
return close_button_hover_color_;
}
void set_close_button_hover_color(Srgb c)
{
close_button_hover_color_ = c;
}
void set_close_button_hover_color_rgb(int r, int g, int b)
{
close_button_hover_color_ = Srgb{r, g, b};
}
SStyle style()
{
return styles_->resolve(style_name_);
}
//region GtkWindow specific API
void set_title(std::string t)
{
text_ = t;
}
std::string get_title()
{
return text_;
}
void set_resizable(bool r)
{
resizable_ = r;
}
bool get_resizable()
{
return resizable_;
}
void set_decorated(bool d)
{
decorated_ = d;
}
bool get_decorated()
{
return decorated_;
}
// Phase 3 (§2.20): BorderStyle / Icon / Position / WindowState.
// BorderStyle none clears the decoration; WindowState drives the
// iconified / maximized / fullscreen flags the widget already has.
void set_border_style(int v)
{
border_style_ = v;
if (v == 1) decorated_ = false;
else if (v == 2 || v == 3) decorated_ = true;
}
int get_border_style()
{
return border_style_;
}
void set_icon(std::string p)
{
icon_ = p;
}
std::string get_icon()
{
return icon_;
}
void set_position(int v)
{
position_ = v;
}
int get_position()
{
return position_;
}
void set_window_state(int v)
{
window_state_ = v;
iconified_ = (v == 1);
maximized_ = (v == 2);
fullscreen_ = (v == 3);
}
int get_window_state()
{
return window_state_;
}
bool is_closed()
{
return closed_;
}
bool is_iconified()
{
return iconified_;
}
bool is_maximized()
{
return maximized_;
}
void close()
{
closed_ = true;
if (on_close)
{
on_close();
}
}
void iconify()
{
iconified_ = true;
if (on_minimize)
{
on_minimize();
}
}
void deiconify()
{
iconified_ = false;
}
void maximize()
{
if (maximized_ || !resizable_)
{
return;
}
saved_x1_ = x1_;
saved_y1_ = y1_;
saved_x2_ = x2_;
saved_y2_ = y2_;
// Grow to fill the host window.
Window root;
int rx, ry;
unsigned int rw, rh, rb, rd;
if (XGetGeometry(display_, window_, &root, &rx, &ry, &rw, &rh, &rb, &rd))
{
x1_ = 0;
y1_ = 0;
x2_ = (int)rw;
y2_ = (int)rh;
}
maximized_ = true;
if (on_maximize)
{
on_maximize();
}
}
void unmaximize()
{
if (!maximized_)
{
return;
}
x1_ = saved_x1_;
y1_ = saved_y1_;
x2_ = saved_x2_;
y2_ = saved_y2_;
maximized_ = false;
}
// The client area below the title bar: where child widgets go.
int content_x()
{
return x1_ + 4;
}
int content_y()
{
return y1_ + (decorated_ ? title_bar_height() : 0) + 4;
}
int content_width()
{
return width() - 8;
}
int content_height()
{
return y2_ - content_y() - 4;
}
//endregion
void draw()
{
if (!visible_ || closed_ || iconified_)
{
return;
}
SStyle st = style();
// The window body.
xf::draw_polished(display_, window_, gc_, st, x1_, y1_, x2_, y2_);
if (decorated_)
{
int tb = title_bar_height();
// Title bar: a band in the outline colour across the top.
XSetForeground(display_, gc_, st.outlineColor_);
XFillRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 4, tb - 2);
// Title text, left aligned in the bar.
SStyle tst = st;
tst.text_color_ = st.fillColor_;
xf::draw_text_left(display_, window_, gc_, tst, x1_ + 10, y1_, y1_ + tb, text_);
// The three window buttons.
const char* glyphs[3] = { "_", "+", "X" };
for (int i = 1; i <= 3; ++i)
{
int bx1, by1, bx2, by2;
button_rect(i, bx1, by1, bx2, by2);
SStyle bst = st;
if (hover_button_ == i)
{
bst.fillColor_ = (i == 3) ? styles_->get_color_Srgb(close_button_hover_color_) : st.fillColor_;
}
xf::draw_polished(display_, window_, gc_, bst, bx1, by1, bx2, by2);
xf::draw_text_centered(display_, window_, gc_, bst, bx1, by1, bx2, by2, glyphs[i - 1]);
}
}
if (focused_)
{
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_ + 1, y1_ + 1, width() - 2, height() - 2);
}
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!contains(x, y) || !enabled_ || button != 1)
{
return false;
}
focused_ = true;
if (decorated_ && y <= y1_ + title_bar_height())
{
for (int i = 1; i <= 3; ++i)
{
int bx1, by1, bx2, by2;
button_rect(i, bx1, by1, bx2, by2);
if (in_rect(x, y, bx1, by1, bx2, by2))
{
if (i == 1)
{
iconify();
}
else if (i == 2)
{
if (maximized_)
{
unmaximize();
}
else
{
maximize();
}
}
else
{
close();
}
return true;
}
}
return true;
}
return true;
}
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)
{
int old = hover_button_;
hover_button_ = 0;
if (decorated_ && contains(x, y))
{
for (int i = 1; i <= 3; ++i)
{
int bx1, by1, bx2, by2;
button_rect(i, bx1, by1, bx2, by2);
if (in_rect(x, y, bx1, by1, bx2, by2))
{
hover_button_ = i;
}
}
}
return hover_button_ != old;
}
bool on_key(KeySym key, const char* str)
{
(void)str;
if (!focused_ || !enabled_)
{
return false;
}
if (key == XK_F4)
{
close();
return true;
}
return false;
}
};
//endregion