Files
cnmcdee b66bc2fbf8 Fix -Wunused-parameter in window widgets after drag removal
The drag removal left on_button_release with an unused button
parameter in GtkWindow and GtkApplicationWindow. Add (void)button.
TForm uses button, so it needed no change. Build is 0 warnings.
2026-11-30 03:29:52 +00:00

842 lines
11 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region GtkApplicationWindow
class GtkApplicationWindow
{
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;
bool closed_ = false;
bool iconified_ = false;
bool maximized_ = false;
int hover_button_ = 0; // 1 min, 2 max, 3 close, 0 none
int saved_x1_ = 0;
int saved_y1_ = 0;
int saved_x2_ = 0;
int saved_y2_ = 0;
// Application chrome: a menu bar under the title bar and a
// status bar along the bottom, like a GtkApplication app.
std::vector<std::string> menus_;
std::string last_menu_; // last menu name clicked
std::string status_text_;
bool show_menubar_ = true;
int title_bar_height()
{
return 28;
}
int menubar_height()
{
return show_menubar_ ? 22 : 0;
}
int statusbar_height()
{
return 20;
}
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;
}
// Menu items are laid out left to right from the menubar's
// left edge, each as wide as its text plus padding.
void menu_rect(int index, int &mx1, int &mx2)
{
SStyle st = style();
mx1 = x1_ + 6;
for (int i = 0; i < index; ++i)
{
mx1 += xf::text_width(st, menus_[i]) + 18;
}
mx2 = mx1 + xf::text_width(st, menus_[index]) + 18;
}
// 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(std::string)> on_menu;
GtkApplicationWindow(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_ = "GtkApplicationWindow";
text_ = "Application";
menus_ = { "File", "Edit", "View", "Help" };
status_text_ = "Ready";
}
~GtkApplicationWindow() {}
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 GtkApplicationWindow specific API
void set_title(std::string t)
{
text_ = t;
}
std::string get_title()
{
return text_;
}
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;
}
void deiconify()
{
iconified_ = false;
}
void maximize()
{
if (maximized_)
{
return;
}
saved_x1_ = x1_;
saved_y1_ = y1_;
saved_x2_ = x2_;
saved_y2_ = y2_;
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;
}
void unmaximize()
{
if (!maximized_)
{
return;
}
x1_ = saved_x1_;
y1_ = saved_y1_;
x2_ = saved_x2_;
y2_ = saved_y2_;
maximized_ = false;
}
void set_show_menubar(bool s)
{
show_menubar_ = s;
}
bool get_show_menubar()
{
return show_menubar_;
}
void add_menu(std::string menu_name)
{
menus_.push_back(menu_name);
}
std::string get_last_menu()
{
return last_menu_;
}
void set_status_text(std::string t)
{
status_text_ = t;
}
std::string get_status_text()
{
return status_text_;
}
// Client area: below title bar + menubar, above the status bar.
int content_x()
{
return x1_ + 4;
}
int content_y()
{
return y1_ + title_bar_height() + menubar_height() + 4;
}
int content_width()
{
return width() - 8;
}
int content_height()
{
return y2_ - statusbar_height() - content_y() - 4;
}
//endregion
void draw()
{
if (!visible_ || closed_ || iconified_)
{
return;
}
SStyle st = style();
xf::draw_polished(display_, window_, gc_, st, x1_, y1_, x2_, y2_);
int tb = title_bar_height();
// Title bar band.
XSetForeground(display_, gc_, st.outlineColor_);
XFillRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 4, tb - 2);
SStyle tst = st;
tst.text_color_ = st.fillColor_;
xf::draw_text_left(display_, window_, gc_, tst, x1_ + 10, y1_, y1_ + tb, text_);
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]);
}
// Menu bar.
if (show_menubar_)
{
int my1 = y1_ + tb;
int my2 = my1 + menubar_height();
XSetForeground(display_, gc_, st.fillColor_);
XFillRectangle(display_, window_, gc_, x1_ + 2, my1, width() - 4, menubar_height());
XSetForeground(display_, gc_, st.lineColor_);
XDrawLine(display_, window_, gc_, x1_ + 2, my2, x2_ - 2, my2);
for (size_t i = 0; i < menus_.size(); ++i)
{
int mx1, mx2;
menu_rect((int)i, mx1, mx2);
if (last_menu_ == menus_[i])
{
XSetForeground(display_, gc_, st.outlineColor_);
XFillRectangle(display_, window_, gc_, mx1, my1 + 1, mx2 - mx1, menubar_height() - 2);
}
xf::draw_text_centered(display_, window_, gc_, st, mx1, my1, mx2, my2, menus_[i]);
}
}
// Status bar along the bottom.
int sy = y2_ - statusbar_height();
XSetForeground(display_, gc_, st.lineColor_);
XDrawLine(display_, window_, gc_, x1_ + 2, sy, x2_ - 2, sy);
xf::draw_text_left(display_, window_, gc_, st, x1_ + 10, sy, y2_, status_text_);
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!contains(x, y) || !enabled_ || button != 1)
{
return false;
}
focused_ = true;
int tb = title_bar_height();
if (y <= y1_ + tb)
{
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;
}
if (show_menubar_ && y <= y1_ + tb + menubar_height())
{
for (size_t i = 0; i < menus_.size(); ++i)
{
int mx1, mx2;
menu_rect((int)i, mx1, mx2);
if (x >= mx1 && x <= mx2)
{
last_menu_ = menus_[i];
status_text_ = menus_[i] + " menu";
if (on_menu)
{
on_menu(menus_[i]);
}
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 (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