780 lines
10 KiB
C++
780 lines
10 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
//region GtkNotebook
|
|
|
|
class GtkNotebook
|
|
|
|
{
|
|
|
|
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_;
|
|
|
|
bool visible_ = true;
|
|
|
|
bool enabled_ = true;
|
|
|
|
bool focused_ = false;
|
|
|
|
std::vector<std::string> pages_;
|
|
|
|
int current_page_ = 0;
|
|
|
|
int tab_height_ = 26;
|
|
|
|
int tab_width_ = 0; // Phase 3 (§2.18): 0 = auto (label + padding)
|
|
|
|
int tab_position_ = 0; // Phase 3 (§2.18): 0 top, 1 bottom, 2 left, 3 right
|
|
|
|
// Colours exposed by the getter/setter pass; defaults are the old hardcoded values.
|
|
|
|
Srgb disabled_text_color_ = {140, 140, 140};
|
|
|
|
Srgb active_tab_color_ = {245, 245, 245};
|
|
|
|
Srgb inactive_tab_color_ = {195, 195, 195};
|
|
|
|
Srgb inactive_tab_text_color_ = {70, 70, 70};
|
|
|
|
public:
|
|
|
|
std::function<void(int)> on_page_changed;
|
|
|
|
GtkNotebook(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_ = "GtkNotebook";
|
|
|
|
text_ = "Notebook";
|
|
|
|
pages_.push_back("General");
|
|
|
|
pages_.push_back("Advanced");
|
|
|
|
pages_.push_back("About");
|
|
|
|
current_page_ = 0;
|
|
|
|
}
|
|
|
|
~GtkNotebook() {}
|
|
|
|
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_ && 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_disabled_text_color()
|
|
|
|
{
|
|
|
|
return disabled_text_color_;
|
|
|
|
}
|
|
|
|
void set_disabled_text_color(Srgb c)
|
|
|
|
{
|
|
|
|
disabled_text_color_ = c;
|
|
|
|
}
|
|
|
|
void set_disabled_text_color_rgb(int r, int g, int b)
|
|
|
|
{
|
|
|
|
disabled_text_color_ = Srgb{r, g, b};
|
|
|
|
}
|
|
|
|
Srgb get_active_tab_color()
|
|
|
|
{
|
|
|
|
return active_tab_color_;
|
|
|
|
}
|
|
|
|
void set_active_tab_color(Srgb c)
|
|
|
|
{
|
|
|
|
active_tab_color_ = c;
|
|
|
|
}
|
|
|
|
void set_active_tab_color_rgb(int r, int g, int b)
|
|
|
|
{
|
|
|
|
active_tab_color_ = Srgb{r, g, b};
|
|
|
|
}
|
|
|
|
Srgb get_inactive_tab_color()
|
|
|
|
{
|
|
|
|
return inactive_tab_color_;
|
|
|
|
}
|
|
|
|
void set_inactive_tab_color(Srgb c)
|
|
|
|
{
|
|
|
|
inactive_tab_color_ = c;
|
|
|
|
}
|
|
|
|
void set_inactive_tab_color_rgb(int r, int g, int b)
|
|
|
|
{
|
|
|
|
inactive_tab_color_ = Srgb{r, g, b};
|
|
|
|
}
|
|
|
|
Srgb get_inactive_tab_text_color()
|
|
|
|
{
|
|
|
|
return inactive_tab_text_color_;
|
|
|
|
}
|
|
|
|
void set_inactive_tab_text_color(Srgb c)
|
|
|
|
{
|
|
|
|
inactive_tab_text_color_ = c;
|
|
|
|
}
|
|
|
|
void set_inactive_tab_text_color_rgb(int r, int g, int b)
|
|
|
|
{
|
|
|
|
inactive_tab_text_color_ = Srgb{r, g, b};
|
|
|
|
}
|
|
|
|
int get_tab_height()
|
|
|
|
{
|
|
|
|
return tab_height_;
|
|
|
|
}
|
|
|
|
void set_tab_height(int v)
|
|
|
|
{
|
|
|
|
tab_height_ = v;
|
|
|
|
}
|
|
|
|
void set_tab_width(int v)
|
|
|
|
{
|
|
|
|
tab_width_ = v;
|
|
|
|
}
|
|
|
|
int get_tab_width()
|
|
|
|
{
|
|
|
|
return tab_width_;
|
|
|
|
}
|
|
|
|
void set_tab_position(int v)
|
|
|
|
{
|
|
|
|
tab_position_ = v;
|
|
|
|
}
|
|
|
|
int get_tab_position()
|
|
|
|
{
|
|
|
|
return tab_position_;
|
|
|
|
}
|
|
|
|
SStyle style()
|
|
|
|
{
|
|
|
|
return styles_->resolve(style_name_);
|
|
|
|
}
|
|
|
|
void append_page(std::string label)
|
|
|
|
{
|
|
|
|
pages_.push_back(label);
|
|
|
|
}
|
|
|
|
void remove_page(int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= (int)pages_.size())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pages_.erase(pages_.begin() + index);
|
|
|
|
if (current_page_ >= (int)pages_.size())
|
|
|
|
{
|
|
|
|
current_page_ = pages_.empty() ? 0 : (int)pages_.size() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void set_current_page(int i)
|
|
|
|
{
|
|
|
|
if (i < 0 || i >= (int)pages_.size() || i == current_page_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
current_page_ = i;
|
|
|
|
if (on_page_changed)
|
|
|
|
{
|
|
|
|
on_page_changed(current_page_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int get_current_page()
|
|
|
|
{
|
|
|
|
return current_page_;
|
|
|
|
}
|
|
|
|
int get_page_count()
|
|
|
|
{
|
|
|
|
return (int)pages_.size();
|
|
|
|
}
|
|
|
|
std::string get_page_label(int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= (int)pages_.size())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return pages_[index];
|
|
|
|
}
|
|
|
|
// Phase 3 (§2.18): rebuild the pages from "Tab 1|Tab 2" text.
|
|
|
|
void set_items(const std::string& pages)
|
|
|
|
{
|
|
|
|
pages_.clear();
|
|
|
|
size_t pos = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
size_t bar = pages.find('|', pos);
|
|
|
|
pages_.push_back(pages.substr(pos, bar == std::string::npos ? bar : bar - pos));
|
|
|
|
if (bar == std::string::npos) break;
|
|
|
|
pos = bar + 1;
|
|
|
|
}
|
|
|
|
if (current_page_ >= (int)pages_.size()) current_page_ = 0;
|
|
|
|
}
|
|
|
|
// Phase 3 (§2.18): tab width/position. Left/right tabs stack
|
|
// vertically in a strip (tab_width_ or 96 wide); the press test
|
|
// already uses tab_rect, so clicks follow the strip.
|
|
|
|
int tab_w_for(int index)
|
|
|
|
{
|
|
|
|
SStyle st = style();
|
|
|
|
if (tab_width_ > 0) return tab_width_;
|
|
|
|
if (index >= 0 && index < (int)pages_.size())
|
|
|
|
{
|
|
|
|
return xf::text_width(st, pages_[index]) + 24;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Rect tab_rect(int index)
|
|
|
|
{
|
|
|
|
if (tab_position_ == 2 || tab_position_ == 3)
|
|
|
|
{
|
|
|
|
int strip = tab_width_ > 0 ? tab_width_ : 96;
|
|
|
|
int tx = tab_position_ == 2 ? x1_ : x2_ - strip;
|
|
|
|
int ty = y1_ + 4 + index * tab_height_;
|
|
|
|
return Rect{tx, ty, tx + strip, ty + tab_height_};
|
|
|
|
}
|
|
|
|
int tx = x1_ + 4;
|
|
|
|
for (int i = 0; i < index && i < (int)pages_.size(); ++i)
|
|
|
|
{
|
|
|
|
tx += tab_w_for(i);
|
|
|
|
}
|
|
|
|
int tw = tab_w_for(index);
|
|
|
|
if (tab_position_ == 1)
|
|
|
|
{
|
|
|
|
return Rect{tx, y2_ - tab_height_, tx + tw, y2_};
|
|
|
|
}
|
|
|
|
return Rect{tx, y1_, tx + tw, y1_ + tab_height_};
|
|
|
|
}
|
|
|
|
Rect page_rect()
|
|
|
|
{
|
|
|
|
if (tab_position_ == 1)
|
|
|
|
{
|
|
|
|
return Rect{x1_, y1_, x2_, y2_ - tab_height_};
|
|
|
|
}
|
|
|
|
if (tab_position_ == 2)
|
|
|
|
{
|
|
|
|
int strip = tab_width_ > 0 ? tab_width_ : 96;
|
|
|
|
return Rect{x1_ + strip, y1_, x2_, y2_};
|
|
|
|
}
|
|
|
|
if (tab_position_ == 3)
|
|
|
|
{
|
|
|
|
int strip = tab_width_ > 0 ? tab_width_ : 96;
|
|
|
|
return Rect{x1_, y1_, x2_ - strip, y2_};
|
|
|
|
}
|
|
|
|
return Rect{x1_, y1_ + tab_height_, x2_, y2_};
|
|
|
|
}
|
|
|
|
void draw()
|
|
|
|
{
|
|
|
|
if (!visible_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SStyle st = style();
|
|
|
|
if (!enabled_)
|
|
|
|
{
|
|
|
|
st.text_color_ = styles_->get_color_Srgb(disabled_text_color_);
|
|
|
|
}
|
|
|
|
Rect pr = page_rect();
|
|
|
|
xf::draw_polished(display_, window_, gc_, st, pr.x1, pr.y1, pr.x2, pr.y2);
|
|
|
|
for (int i = 0; i < (int)pages_.size(); ++i)
|
|
|
|
{
|
|
|
|
Rect tr = tab_rect(i);
|
|
|
|
SStyle tst = st;
|
|
|
|
if (i == current_page_)
|
|
|
|
{
|
|
|
|
tst.fillColor_ = styles_->get_color_Srgb(active_tab_color_);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tst.fillColor_ = styles_->get_color_Srgb(inactive_tab_color_);
|
|
|
|
tst.text_color_ = styles_->get_color_Srgb(inactive_tab_text_color_);
|
|
|
|
}
|
|
|
|
if (!enabled_)
|
|
|
|
{
|
|
|
|
tst.text_color_ = styles_->get_color_Srgb(disabled_text_color_);
|
|
|
|
}
|
|
|
|
xf::fill_rounded_rect(display_, window_, gc_, tst, tr.x1, tr.y1 + 2, tr.x2, tr.y2 + 4);
|
|
|
|
xf::draw_rounded_outline(display_, window_, gc_, tst, tr.x1, tr.y1 + 2, tr.x2, tr.y2 + 4);
|
|
|
|
xf::draw_text_centered(display_, window_, gc_, tst, tr.x1, tr.y1, tr.x2, tr.y2, pages_[i]);
|
|
|
|
}
|
|
|
|
if (!pages_.empty())
|
|
|
|
{
|
|
|
|
xf::draw_text_centered(display_, window_, gc_, st, pr.x1, pr.y1, pr.x2, pr.y2, pages_[current_page_]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xf::draw_text_centered(display_, window_, gc_, st, pr.x1, pr.y1, pr.x2, pr.y2, "(no pages)");
|
|
|
|
}
|
|
|
|
if (focused_)
|
|
|
|
{
|
|
|
|
XSetForeground(display_, gc_, st.lineColor_);
|
|
|
|
XDrawRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 4, height() - 4);
|
|
|
|
}
|
|
|
|
XFlush(display_);
|
|
|
|
}
|
|
|
|
bool on_button_press(int x, int y, unsigned int button)
|
|
|
|
{
|
|
|
|
if (!contains(x, y) || !enabled_ || button != 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
focused_ = true;
|
|
|
|
for (int i = 0; i < (int)pages_.size(); ++i)
|
|
|
|
{
|
|
|
|
Rect tr = tab_rect(i);
|
|
|
|
if (x >= tr.x1 && x <= tr.x2 && y >= tr.y1 && y <= tr.y2)
|
|
|
|
{
|
|
|
|
set_current_page(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)
|
|
|
|
{
|
|
|
|
(void)x;
|
|
|
|
(void)y;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool on_key(KeySym key, const char* str)
|
|
|
|
{
|
|
|
|
(void)str;
|
|
|
|
if (!focused_ || !enabled_ || pages_.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (key == XK_Right)
|
|
|
|
{
|
|
|
|
set_current_page((current_page_ + 1) % (int)pages_.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (key == XK_Left)
|
|
|
|
{
|
|
|
|
set_current_page((current_page_ + (int)pages_.size() - 1) % (int)pages_.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//endregion
|