Files
lightgui/widgets/components/TSavePictureDialog/TSavePictureDialog.h

1232 lines
19 KiB
C++

#pragma once
#include "core/xframe_core.h"
//region TSavePictureDialog
class TSavePictureDialog
{
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;
// Lazarus modal results
static const int mrNone = 0;
static const int mrOk = 1;
static const int mrCancel = 2;
std::vector<std::string> files_;
std::vector<std::string> sizes_; // fake pixel dimensions per file
int selected_ = 0;
int top_index_ = 0;
std::string directory_;
std::string filter_;
std::string save_name_;
int save_cursor_ = 0;
bool edit_focus_ = true;
int result_ = 0;
bool in_rect(int px, int py, int rx1, int ry1, int rx2, int ry2)
{
return px >= rx1 && px <= rx2 && py >= ry1 && py <= ry2;
}
int list_x2()
{
return x1_ + (width() - 24) * 52 / 100;
}
void draw_chrome()
{
SStyle st = style();
xf::draw_polished(display_, window_, gc_, st, x1_, y1_, x2_, y2_);
XSetForeground(display_, gc_, st.outlineColor_);
XFillRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 4, 22);
SStyle tst = st;
tst.text_color_ = styles_->get_color_Srgb(title_text_color_);
xf::draw_text_left(display_, window_, gc_, tst, x1_ + 8, y1_ + 2, y1_ + 24, text_);
XSetForeground(display_, gc_, styles_->get_color_Srgb(title_bar_color_));
XFillRectangle(display_, window_, gc_, x2_ - 24, y1_ + 4, 18, 18);
xf::draw_text_centered(display_, window_, gc_, tst, x2_ - 24, y1_ + 4, x2_ - 6, y1_ + 22, "X");
}
void draw_mini_button(int bx1, int by1, int bx2, int by2, const std::string& cap, bool down)
{
SStyle st = style();
if (down)
{
unsigned long swap = st.fillColor_;
st.fillColor_ = st.outlineColor_;
st.outlineColor_ = swap;
}
xf::draw_polished(display_, window_, gc_, st, bx1, by1, bx2, by2);
xf::draw_text_centered(display_, window_, gc_, st, bx1, by1, bx2, by2, cap);
}
void draw_edit(int ex1, int ey1, int ex2, int ey2, const std::string& s, int cursor, bool foc)
{
SStyle st = style();
XSetForeground(display_, gc_, styles_->get_color_Srgb(edit_background_color_));
XFillRectangle(display_, window_, gc_, ex1, ey1, ex2 - ex1, ey2 - ey1);
XSetForeground(display_, gc_, st.lineColor_);
xf::set_line_width(display_, gc_, 1);
XDrawRectangle(display_, window_, gc_, ex1, ey1, ex2 - ex1, ey2 - ey1);
xf::draw_text_left(display_, window_, gc_, st, ex1 + 5, ey1, ey2, s);
if (foc)
{
int cx = ex1 + 5 + xf::text_width(st, s.substr(0, (size_t)cursor));
XSetForeground(display_, gc_, st.text_color_);
XDrawLine(display_, window_, gc_, cx, ey1 + 3, cx, ey2 - 3);
}
}
bool edit_key(std::string& buf, int& cur, KeySym key, const char* str)
{
if (key == XK_BackSpace)
{
if (cur > 0)
{
buf.erase((size_t)cur - 1, 1);
cur--;
}
return true;
}
if (key == XK_Delete)
{
if (cur < (int)buf.size())
{
buf.erase((size_t)cur, 1);
}
return true;
}
if (key == XK_Left)
{
if (cur > 0)
{
cur--;
}
return true;
}
if (key == XK_Right)
{
if (cur < (int)buf.size())
{
cur++;
}
return true;
}
if (key == XK_Home)
{
cur = 0;
return true;
}
if (key == XK_End)
{
cur = (int)buf.size();
return true;
}
if (str && str[0] >= 32 && str[0] < 127)
{
buf.insert((size_t)cur, 1, str[0]);
cur++;
return true;
}
return false;
}
// Placeholder landscape glyph, varied per selected file.
void draw_preview_glyph(int px1, int py1, int px2, int py2, int seed)
{
int w = px2 - px1;
int h = py2 - py1;
XSetForeground(display_, gc_, styles_->get_color_rgb(150 + (seed * 13) % 60, 195, 235));
XFillRectangle(display_, window_, gc_, px1, py1, w, h * 62 / 100);
int sun_d = std::max(10, w / 7);
int sun_x = px1 + w / 6 + (seed % 4) * (w / 6);
XSetForeground(display_, gc_, styles_->get_color_Srgb(preview_sun_color_));
XFillArc(display_, window_, gc_, sun_x, py1 + h / 8, sun_d, sun_d, 0, 360 * 64);
int base = py1 + h * 62 / 100;
XSetForeground(display_, gc_, styles_->get_color_Srgb(preview_mountain_color_));
for (int r = 0; r < h / 3; ++r)
{
int half = (r * w / 3) / std::max(1, h / 3);
int cx = px1 + w / 3;
XDrawLine(display_, window_, gc_, cx - half, base - h / 3 + r + h / 3, cx + half, base - h / 3 + r + h / 3);
}
XSetForeground(display_, gc_, styles_->get_color_rgb(90 + (seed * 7) % 40, 165, 90));
XFillRectangle(display_, window_, gc_, px1, base, w, py2 - base);
XSetForeground(display_, gc_, styles_->get_color_Srgb(preview_hill_color_));
int hx = px1 + w * 2 / 3;
int hh = h / 4;
for (int r = 0; r < hh; ++r)
{
int half = (r * w / 4) / std::max(1, hh);
XDrawLine(display_, window_, gc_, hx - half, base + hh - r, hx + half, base + hh - r);
}
}
// Colours exposed by the getter/setter pass; defaults are the old hardcoded values.
Srgb title_text_color_ = {255, 255, 255};
Srgb title_bar_color_ = {200, 60, 50};
Srgb edit_background_color_ = {255, 255, 255};
Srgb preview_sun_color_ = {250, 215, 60};
Srgb preview_mountain_color_ = {110, 130, 160};
Srgb preview_hill_color_ = {60, 130, 70};
Srgb list_background_color_ = {255, 255, 255};
Srgb selected_text_color_ = {255, 255, 255};
Srgb picture_icon_sky_color_ = {150, 195, 235};
Srgb picture_icon_grass_color_ = {90, 165, 90};
Srgb preview_background_color_ = {255, 255, 255};
public:
std::function<void()> on_click;
TSavePictureDialog(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_ = "TSavePictureDialog";
text_ = "Save Picture As";
directory_ = "/home/user/Pictures";
filter_ = "Picture files (*.png;*.jpg;*.bmp;*.gif;*.ico)";
files_ = {"sunset.png", "mountain.jpg", "river.bmp", "forest.gif", "city.png", "beach.jpg", "desert.png", "lake.jpg", "harbour.png"};
sizes_ = {"1920 x 1080", "1600 x 1200", "800 x 600", "640 x 480", "2048 x 1365", "1280 x 853", "1024 x 768", "1440 x 900", "900 x 600"};
selected_ = 0;
save_name_ = "new-picture.png";
save_cursor_ = (int)save_name_.size();
}
~TSavePictureDialog() {}
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_title_text_color()
{
return title_text_color_;
}
void set_title_text_color(Srgb c)
{
title_text_color_ = c;
}
void set_title_text_color_rgb(int r, int g, int b)
{
title_text_color_ = Srgb{r, g, b};
}
Srgb get_title_bar_color()
{
return title_bar_color_;
}
void set_title_bar_color(Srgb c)
{
title_bar_color_ = c;
}
void set_title_bar_color_rgb(int r, int g, int b)
{
title_bar_color_ = Srgb{r, g, b};
}
Srgb get_edit_background_color()
{
return edit_background_color_;
}
void set_edit_background_color(Srgb c)
{
edit_background_color_ = c;
}
void set_edit_background_color_rgb(int r, int g, int b)
{
edit_background_color_ = Srgb{r, g, b};
}
Srgb get_preview_sun_color()
{
return preview_sun_color_;
}
void set_preview_sun_color(Srgb c)
{
preview_sun_color_ = c;
}
void set_preview_sun_color_rgb(int r, int g, int b)
{
preview_sun_color_ = Srgb{r, g, b};
}
Srgb get_preview_mountain_color()
{
return preview_mountain_color_;
}
void set_preview_mountain_color(Srgb c)
{
preview_mountain_color_ = c;
}
void set_preview_mountain_color_rgb(int r, int g, int b)
{
preview_mountain_color_ = Srgb{r, g, b};
}
Srgb get_preview_hill_color()
{
return preview_hill_color_;
}
void set_preview_hill_color(Srgb c)
{
preview_hill_color_ = c;
}
void set_preview_hill_color_rgb(int r, int g, int b)
{
preview_hill_color_ = Srgb{r, g, b};
}
Srgb get_list_background_color()
{
return list_background_color_;
}
void set_list_background_color(Srgb c)
{
list_background_color_ = c;
}
void set_list_background_color_rgb(int r, int g, int b)
{
list_background_color_ = Srgb{r, g, b};
}
Srgb get_selected_text_color()
{
return selected_text_color_;
}
void set_selected_text_color(Srgb c)
{
selected_text_color_ = c;
}
void set_selected_text_color_rgb(int r, int g, int b)
{
selected_text_color_ = Srgb{r, g, b};
}
Srgb get_picture_icon_sky_color()
{
return picture_icon_sky_color_;
}
void set_picture_icon_sky_color(Srgb c)
{
picture_icon_sky_color_ = c;
}
void set_picture_icon_sky_color_rgb(int r, int g, int b)
{
picture_icon_sky_color_ = Srgb{r, g, b};
}
Srgb get_picture_icon_grass_color()
{
return picture_icon_grass_color_;
}
void set_picture_icon_grass_color(Srgb c)
{
picture_icon_grass_color_ = c;
}
void set_picture_icon_grass_color_rgb(int r, int g, int b)
{
picture_icon_grass_color_ = Srgb{r, g, b};
}
Srgb get_preview_background_color()
{
return preview_background_color_;
}
void set_preview_background_color(Srgb c)
{
preview_background_color_ = c;
}
void set_preview_background_color_rgb(int r, int g, int b)
{
preview_background_color_ = Srgb{r, g, b};
}
std::string get_directory()
{
return directory_;
}
void set_directory(std::string v)
{
directory_ = v;
}
SStyle style()
{
return styles_->resolve(style_name_);
}
std::string get_filename()
{
if (save_name_.empty())
{
return directory_;
}
if (save_name_[0] == '/')
{
return save_name_;
}
return directory_ + "/" + save_name_;
}
void set_filename(std::string f)
{
save_name_ = f;
save_cursor_ = (int)save_name_.size();
}
std::string get_filter()
{
return filter_;
}
int get_result()
{
return result_;
}
void draw()
{
if (!visible_)
{
return;
}
SStyle st = style();
draw_chrome();
xf::draw_text_left(display_, window_, gc_, st, x1_ + 12, y1_ + 28, y1_ + 50, "Save in: " + directory_);
//region File list (left)
int lx1 = x1_ + 12;
int ly1 = y1_ + 54;
int lx2 = list_x2();
int ly2 = y2_ - 76;
XSetForeground(display_, gc_, styles_->get_color_Srgb(list_background_color_));
XFillRectangle(display_, window_, gc_, lx1, ly1, lx2 - lx1, ly2 - ly1);
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, lx1, ly1, lx2 - lx1, ly2 - ly1);
int rows = (ly2 - ly1) / 18;
for (int i = top_index_; i < (int)files_.size() && i < top_index_ + rows; ++i)
{
int ry = ly1 + (i - top_index_) * 18;
SStyle rst = st;
if (i == selected_)
{
XSetForeground(display_, gc_, st.outlineColor_);
XFillRectangle(display_, window_, gc_, lx1 + 1, ry, lx2 - lx1 - 2, 18);
rst.text_color_ = styles_->get_color_Srgb(selected_text_color_);
}
XSetForeground(display_, gc_, styles_->get_color_Srgb(picture_icon_sky_color_));
XFillRectangle(display_, window_, gc_, lx1 + 4, ry + 4, 12, 10);
XSetForeground(display_, gc_, styles_->get_color_Srgb(picture_icon_grass_color_));
XFillRectangle(display_, window_, gc_, lx1 + 4, ry + 10, 12, 4);
xf::draw_text_left(display_, window_, gc_, rst, lx1 + 20, ry, ry + 18, files_[(size_t)i]);
}
//endregion
//region Preview (right)
int px1 = lx2 + 10;
int py1 = ly1;
int px2 = x2_ - 12;
int py2 = ly2 - 22;
XSetForeground(display_, gc_, styles_->get_color_Srgb(preview_background_color_));
XFillRectangle(display_, window_, gc_, px1, py1, px2 - px1, py2 - py1);
draw_preview_glyph(px1 + 3, py1 + 3, px2 - 3, py2 - 3, selected_);
XSetForeground(display_, gc_, st.lineColor_);
xf::set_line_width(display_, gc_, 1);
XDrawRectangle(display_, window_, gc_, px1, py1, px2 - px1, py2 - py1);
std::string cap = files_[(size_t)selected_] + " " + sizes_[(size_t)selected_];
xf::draw_text_centered(display_, window_, gc_, st, px1, py2 + 2, px2, ly2, cap);
//endregion
xf::draw_text_left(display_, window_, gc_, st, x1_ + 12, y2_ - 68, y2_ - 46, "File name:");
draw_edit(x1_ + 96, y2_ - 68, x2_ - 12, y2_ - 46, save_name_, save_cursor_, edit_focus_);
xf::draw_text_left(display_, window_, gc_, st, x1_ + 12, y2_ - 40, y2_ - 18, filter_);
draw_mini_button(x2_ - 188, y2_ - 34, x2_ - 100, y2_ - 10, "Save", false);
draw_mini_button(x2_ - 92, y2_ - 34, x2_ - 12, y2_ - 10, "Cancel", false);
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!contains(x, y) || !enabled_)
{
return false;
}
focused_ = true;
if (button == 1 && in_rect(x, y, x2_ - 24, y1_ + 4, x2_ - 6, y1_ + 22))
{
result_ = mrCancel;
return true;
}
int lx1 = x1_ + 12;
int ly1 = y1_ + 54;
int lx2 = list_x2();
int ly2 = y2_ - 76;
if (in_rect(x, y, lx1, ly1, lx2, ly2))
{
int rows = (ly2 - ly1) / 18;
if (button == 4)
{
if (top_index_ > 0)
{
top_index_--;
}
return true;
}
if (button == 5)
{
if (top_index_ + rows < (int)files_.size())
{
top_index_++;
}
return true;
}
if (button == 1)
{
int idx = top_index_ + (y - ly1) / 18;
if (idx >= 0 && idx < (int)files_.size())
{
selected_ = idx;
save_name_ = files_[(size_t)idx];
save_cursor_ = (int)save_name_.size();
edit_focus_ = true;
}
return true;
}
return false;
}
if (button != 1)
{
return false;
}
if (in_rect(x, y, x1_ + 96, y2_ - 68, x2_ - 12, y2_ - 46))
{
edit_focus_ = true;
save_cursor_ = (int)save_name_.size();
return true;
}
if (in_rect(x, y, x2_ - 188, y2_ - 34, x2_ - 100, y2_ - 10))
{
if (!save_name_.empty())
{
result_ = mrOk;
}
return true;
}
if (in_rect(x, y, x2_ - 92, y2_ - 34, x2_ - 12, y2_ - 10))
{
result_ = mrCancel;
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)
{
if (!focused_ || !enabled_)
{
return false;
}
if (edit_focus_)
{
if (key == XK_Return)
{
if (!save_name_.empty())
{
result_ = mrOk;
}
return true;
}
if (key == XK_Escape)
{
result_ = mrCancel;
return true;
}
return edit_key(save_name_, save_cursor_, key, str);
}
if (key == XK_Up)
{
if (selected_ > 0)
{
selected_--;
}
return true;
}
if (key == XK_Down)
{
if (selected_ < (int)files_.size() - 1)
{
selected_++;
}
return true;
}
if (key == XK_Return)
{
if (!save_name_.empty())
{
result_ = mrOk;
}
return true;
}
if (key == XK_Escape)
{
result_ = mrCancel;
return true;
}
return false;
}
};
//endregion