Files
lightgui/widgets/components/GtkProgressBar/GtkProgressBar.h

619 lines
8.5 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include <cstdio>
//region GtkProgressBar
// A trough with a filled portion showing fraction_ (0..1), with an
// optional text overlay. In pulse mode a block slides back and
// forth instead — pulse() advances it.
class GtkProgressBar
{
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;
double fraction_ = 0.45;
bool show_text_ = true;
bool custom_text_ = false; // text_ set by the caller wins
bool pulsing_ = false;
double pulse_pos_ = 0.0; // 0..1 position of the sliding block
int pulse_dir_ = 1;
bool inverted_ = false;
// Phase 3 (§2.13): Position/Min/Max/Step over fraction_.
double min_ = 0.0;
double max_ = 100.0;
double position_ = 45.0;
double step_ = 1.0;
// Getter/setter pass: colours formerly hardcoded in draw()/helpers.
Srgb trough_color_{225, 225, 225};
Srgb bar_color_{52, 101, 164};
Srgb disabled_fill_color_{150, 150, 150};
public:
GtkProgressBar(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_ = "GtkProgressBar";
text_ = "";
}
~GtkProgressBar() {}
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;
custom_text_ = true;
}
std::string get_text()
{
return text_;
}
SStyle style()
{
return styles_->resolve(style_name_);
}
//region GtkProgressBar API
void set_fraction(double f)
{
fraction_ = std::max(0.0, std::min(1.0, f));
position_ = min_ + fraction_ * (max_ - min_);
}
double get_fraction()
{
return fraction_;
}
void set_show_text(bool s)
{
show_text_ = s;
}
bool get_show_text()
{
return show_text_;
}
void set_pulsing(bool p)
{
pulsing_ = p;
}
bool get_pulsing()
{
return pulsing_;
}
void set_inverted(bool i)
{
inverted_ = i;
}
bool get_inverted()
{
return inverted_;
}
// Phase 3 (§2.13): Position/Min/Max/Step as a convenience over
// fraction_ — fraction_ is always (position_ - min_) / (max_ - min_).
void recompute_fraction()
{
if (max_ > min_)
{
fraction_ = std::max(0.0, std::min(1.0, (position_ - min_) / (max_ - min_)));
}
else
{
fraction_ = 0.0;
}
}
void set_position(double p)
{
position_ = p;
recompute_fraction();
}
double get_position()
{
return position_;
}
void set_min(double v)
{
min_ = v;
recompute_fraction();
}
double get_min()
{
return min_;
}
void set_max(double v)
{
max_ = v;
recompute_fraction();
}
double get_max()
{
return max_;
}
void set_step(double s)
{
step_ = s;
}
double get_step()
{
return step_;
}
void step_it()
{
set_position(position_ + step_);
}
// One step of activity-mode animation: the block bounces.
void pulse()
{
pulsing_ = true;
pulse_pos_ += 0.06 * pulse_dir_;
if (pulse_pos_ > 0.8)
{
pulse_pos_ = 0.8;
pulse_dir_ = -1;
}
if (pulse_pos_ < 0.0)
{
pulse_pos_ = 0.0;
pulse_dir_ = 1;
}
}
std::string display_text()
{
if (custom_text_ && !text_.empty())
{
return text_;
}
char buf[24];
std::snprintf(buf, sizeof(buf), "%d%%", (int)(fraction_ * 100.0 + 0.5));
return std::string(buf);
}
//endregion
//region GtkProgressBar colours (getter/setter pass: formerly hardcoded in draw()/helpers)
void set_trough_color(Srgb c) { trough_color_ = c; }
Srgb get_trough_color() { return trough_color_; }
void set_trough_color_rgb(int r, int g, int b) { trough_color_ = Srgb{r, g, b}; }
void set_bar_color(Srgb c) { bar_color_ = c; }
Srgb get_bar_color() { return bar_color_; }
void set_bar_color_rgb(int r, int g, int b) { bar_color_ = Srgb{r, g, b}; }
void set_disabled_fill_color(Srgb c) { disabled_fill_color_ = c; }
Srgb get_disabled_fill_color() { return disabled_fill_color_; }
void set_disabled_fill_color_rgb(int r, int g, int b) { disabled_fill_color_ = Srgb{r, g, b}; }
//endregion
void draw()
{
if (!visible_)
{
return;
}
SStyle st = style();
// Trough: light sunken body.
SStyle trough = st;
trough.fillColor_ = styles_->get_color_rgb(trough_color_.r, trough_color_.g, trough_color_.b);
xf::draw_polished(display_, window_, gc_, trough, x1_, y1_, x2_, y2_);
unsigned long fill = enabled_ ? styles_->get_color_rgb(bar_color_.r, bar_color_.g, bar_color_.b) : styles_->get_color_rgb(disabled_fill_color_.r, disabled_fill_color_.g, disabled_fill_color_.b);
int inner_w = width() - 4;
if (pulsing_)
{
int block_w = std::max(10, inner_w / 5);
int bx = x1_ + 2 + (int)((inner_w - block_w) * pulse_pos_);
XSetForeground(display_, gc_, fill);
XFillRectangle(display_, window_, gc_, bx, y1_ + 2, block_w, height() - 4);
}
else if (fraction_ > 0.0)
{
int fw = (int)(inner_w * fraction_);
XSetForeground(display_, gc_, fill);
if (inverted_)
{
XFillRectangle(display_, window_, gc_, x2_ - 2 - fw, y1_ + 2, fw, height() - 4);
}
else
{
XFillRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, fw, height() - 4);
}
}
if (show_text_ && !pulsing_)
{
xf::draw_text_centered(display_, window_, gc_, st, x1_, y1_, x2_, y2_, display_text());
}
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!contains(x, y) || button != 1)
{
return false;
}
focused_ = true;
// Clicking the bar sets the fraction, like a slider.
pulsing_ = false;
double f = (double)(x - (x1_ + 2)) / std::max(1, width() - 4);
set_fraction(inverted_ ? 1.0 - f : f);
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_)
{
return false;
}
if (key == XK_Right || key == XK_Up)
{
pulsing_ = false;
set_fraction(fraction_ + 0.05);
return true;
}
if (key == XK_Left || key == XK_Down)
{
pulsing_ = false;
set_fraction(fraction_ - 0.05);
return true;
}
return false;
}
};
//endregion