Skip to content

bindings/cpp/desktop_gui.cpp

C++98 API source. Preserve the bindings/cpp/ layout when downloading multiple files.

Source files · Download desktop_gui.cpp

#include "desktop_gui.hpp"
#include <algorithm>
#include <cstdio>

namespace gaspardos
{
    namespace desktop
    {
        void TextInput::set(const std::string& text)
        {
            value  = text.substr(0, 4095);
            cursor = value.size();
        }

        void TextInput::key(uint32_t key)
        {
            if (key == 8 && cursor)
            {
                std::size_t start = previousChar(value, cursor);
                value.erase(start, cursor - start);
                cursor = start;
            }
            else if (key == 127 || key == 0xF805)
            {
                if (cursor < value.size())
                    value.erase(cursor, nextChar(value, cursor) - cursor);
            }
            else if (key == 0xF800)
                cursor = previousChar(value, cursor);
            else if (key == 0xF801)
                cursor = nextChar(value, cursor);
            else if (key == 0xF806)
                cursor = 0;
            else if (key == 0xF807)
                cursor = value.size();
            else if (key >= 32 && !(key >= 0xF800 && key <= 0xFFFF) && value.size() < 4091)
            {
                std::string encoded = utf8(key);
                value.insert(cursor, encoded);
                cursor += encoded.size();
            }
        }

        std::string TextInput::visible(unsigned columns, bool focused) const
        {
            std::string display = value;
            if (focused)
                display.insert(cursor, "|");
            std::size_t begin = cursor >= columns ? cursor - columns + 1 : 0;
            while (begin < display.size()
                   && (static_cast<unsigned char>(display[begin]) & 0xC0) == 0x80)
                ++begin;
            return display.substr(begin, columns);
        }

        void PaintQueue::clear()
        {
            commands_.clear();
            position_ = 0;
        }

        void PaintQueue::fill(int x, int y, int width, int height, uint32_t color)
        {
            if (width > 0 && height > 0)
                commands_.push_back(Command(false, x, y, width, height, color));
        }

        void PaintQueue::text(
            int x, int y, const std::string& value, uint32_t color, unsigned columns)
        {
            std::string s = value.substr(0, std::min<unsigned>(columns, 120));
            // Avoid ending a native text command inside a UTF-8 sequence.
            if (s.size() < value.size())
                while (!s.empty() && (static_cast<unsigned char>(value[s.size()]) & 0xC0) == 0x80)
                    s.resize(s.size() - 1);
            commands_.push_back(Command(true, x, y, 0, 0, color, s));
        }

        void PaintQueue::button(int x, int y, int width, const std::string& title, bool selected)
        {
            fill(x, y, width, 26, selected ? 0xC06020 : 0x707070);
            fill(x + 1, y + 1, width - 2, 24, 0xD8E9EC);
            text(x + 7, y + 6, title, 0x202020, (width - 12) / 8);
        }

        void PaintQueue::input(int x, int y, int width, const TextInput& input, bool focused)
        {
            fill(x, y, width, 26, focused ? 0xC06020 : 0x707070);
            fill(x + 1, y + 1, width - 2, 24, 0xFFFFFF);
            text(
                x + 6, y + 6, input.visible((width - 12) / 8, focused), 0x202020, (width - 12) / 8);
        }

        int64_t PaintQueue::paint(gui::Painter& painter)
        {
            for (unsigned budget = 0; budget < 12 && position_ < commands_.size(); ++budget)
            {
                const Command& c = commands_[position_];
                int64_t        r = c.text ? painter.text(c.x, c.y, c.color, c.label.c_str())
                                          : painter.fill(c.x, c.y, c.w, c.h, c.color);
                if (r < 0)
                    return r;
                ++position_;
            }
            return 0;
        }

        FileDialog::FileDialog(FileAccess& access)
            : explorer_(access)
            , scroll_(0)
            , volumeScroll_(0)
            , focus_(2)
            , pressed_(0)
            , lastClicked_(-1)
            , lastClick_(0)
            , active_(false)
            , dirty_(true)
        {
        }

        void FileDialog::begin(FileExplorer::Mode mode,
                               const std::string& initial,
                               const std::string& suggested,
                               bool               overwrite)
        {
            explorer_.begin(mode, initial, overwrite);
            name_.set(suggested);
            address_.set(explorer_.directory());
            lastDirectory_ = explorer_.directory();
            scroll_ = volumeScroll_ = 0;
            focus_                  = mode == FileExplorer::SelectFolder ? 1 : 2;
            pressed_                = 0;
            lastClicked_            = -1;
            active_ = dirty_ = true;
            queue_.clear();
        }

        void FileDialog::synchronize()
        {
            if (lastDirectory_ != explorer_.directory())
            {
                lastDirectory_ = explorer_.directory();
                address_.set(lastDirectory_);
                scroll_      = 0;
                lastClicked_ = -1;
                if (explorer_.mode() != FileExplorer::SaveFile)
                    name_.set("");
            }
            if (explorer_.finished())
                active_ = false;
        }

        int FileDialog::hit(int x, int y) const
        {
            if (y >= 96 && y < 122)
            {
                if (x >= 164 && x < 746)
                    return 1;
                if (x >= 754 && x < 824)
                    return 10;
                if (x >= 832 && x < 922)
                    return 11;
            }
            if (y >= 548 && y < 574)
            {
                if (x >= 170 && x < 922)
                    return 2;
            }
            if (y >= 586 && y < 612)
            {
                if (x >= 78 && x < 218)
                    return 12;
                if (x >= 650 && x < 788)
                    return 13;
                if (x >= 800 && x < 922)
                    return 14;
            }
            if (y >= 510 && y < 536)
            {
                if (x >= 78 && x < 142)
                    return 15;
                if (x >= 150 && x < 218)
                    return 16;
                if (x >= 750 && x < 824)
                    return 17;
                if (x >= 832 && x < 922)
                    return 18;
            }
            return 0;
        }

        void FileDialog::action(int id)
        {
            if (id == 10)
                explorer_.navigate(parentPath(explorer_.directory()));
            else if (id == 11)
                explorer_.navigate(explorer_.directory());
            else if (id == 12)
                explorer_.createFolder(name_.value.empty() ? "Nouveau dossier" : name_.value);
            else if (id == 13)
                explorer_.accept(name_.value);
            else if (id == 14)
                explorer_.cancel();
            else if (id == 15)
            {
                if (volumeScroll_ >= 16)
                    volumeScroll_ -= 16;
            }
            else if (id == 16)
            {
                if (volumeScroll_ + 16 < explorer_.volumes.size())
                    volumeScroll_ += 16;
            }
            else if (id == 17)
            {
                scroll_ = scroll_ >= 17 ? scroll_ - 17 : 0;
            }
            else if (id == 18)
            {
                if (scroll_ + 17 < explorer_.entries().size())
                    scroll_ += 17;
            }
            synchronize();
            dirty_ = true;
        }

        void FileDialog::event(const GO64_Event& e, uint64_t ticks)
        {
            if (!active_)
                return;
            if (e.type == 1 || e.type == 100 || e.type == 102)
            {
                dirty_ = true;
                if (e.type == 102)
                    pressed_ = 0;
            }
            if (e.type == 15)
            {
                explorer_.cancel();
                synchronize();
                return;
            }
            if (e.type == 2 && (e.buttons & 1))
            {
                int control = hit(e.x, e.y);
                if (control == 1 || control == 2)
                {
                    focus_   = control;
                    pressed_ = 0;
                }
                else
                    pressed_ = control;
                if (e.x >= 78 && e.x < 236 && e.y >= 158 && e.y < 478)
                {
                    std::size_t i = volumeScroll_ + static_cast<unsigned>((e.y - 158) / 20);
                    if (i < explorer_.volumes.size())
                        explorer_.navigate(explorer_.volumes[i].path);
                    synchronize();
                }
                else if (e.x >= 254 && e.x < 922 && e.y >= 158 && e.y < 498 && !explorer_.loading())
                {
                    int index = static_cast<int>(scroll_) + (e.y - 158) / 20;
                    if (index >= 0 && static_cast<std::size_t>(index) < explorer_.entries().size())
                    {
                        explorer_.select(index);
                        const FileEntry& entry = explorer_.entries()[index];
                        if (!entry.directory)
                            name_.set(entry.name);
                        if (index == lastClicked_ && ticks - lastClick_ < 500)
                        {
                            explorer_.activate(index);
                            lastClicked_ = -1;
                            synchronize();
                        }
                        else
                        {
                            lastClicked_ = index;
                            lastClick_   = ticks;
                        }
                    }
                }
                dirty_ = true;
            }
            if (e.type == 3 && !(e.buttons & 1) && pressed_)
            {
                int id   = pressed_;
                pressed_ = 0;
                if (hit(e.x, e.y) == id)
                    action(id);
                dirty_ = true;
            }
            if (e.type == 8)
            {
                if (e.key == 27)
                {
                    explorer_.cancel();
                    synchronize();
                    return;
                }
                if (e.key == 9)
                    focus_ = focus_ == 1 ? 2 : 1;
                else if (e.key == 13)
                {
                    if (focus_ == 1)
                        explorer_.navigate(address_.value);
                    else
                        explorer_.accept(name_.value);
                }
                else if (e.key == 0xF808)
                    action(17);
                else if (e.key == 0xF809)
                    action(18);
                else if (focus_ == 1)
                    address_.key(e.key);
                else
                    name_.key(e.key);
                synchronize();
                dirty_ = true;
            }
        }

        void FileDialog::tick()
        {
            if (active_ && explorer_.tick())
            {
                synchronize();
                dirty_ = true;
            }
        }

        void FileDialog::snapshot()
        {
            queue_.clear();
            dirty_ = false;
            queue_.fill(0, 0, 1000, 700, 0xA0A0A0);
            queue_.fill(60, 50, 880, 600, 0xD8E9EC);
            queue_.fill(60, 50, 880, 30, 0xC06018);
            std::string title = explorer_.mode() == FileExplorer::SaveFile ? "Enregistrer sous"
                                : explorer_.mode() == FileExplorer::SelectFolder
                                    ? "Choisir un dossier"
                                    : "Ouvrir un fichier";
            queue_.text(74, 60, title, 0xFFFFFF);
            queue_.text(78, 104, "Dossier :");
            queue_.input(164, 96, 582, address_, focus_ == 1);
            queue_.button(754, 96, 70, "Haut");
            queue_.button(832, 96, 90, "Actualiser");
            queue_.fill(78, 136, 158, 368, 0xE7D5BD);
            queue_.text(88, 142, "Ordinateur");
            for (unsigned row = 0; row < 16 && volumeScroll_ + row < explorer_.volumes.size();
                 ++row)
                queue_.text(
                    88, 158 + row * 20, explorer_.volumes[volumeScroll_ + row].name, 0x603020, 17);
            queue_.fill(254, 136, 668, 374, 0xFFFFFF);
            queue_.fill(254, 136, 668, 20, 0xD8D8D8);
            queue_.text(262, 140, "Nom");
            queue_.text(766, 140, "Type / taille");
            const std::vector<FileEntry>& entries = explorer_.entries();
            for (unsigned row = 0; row < 17 && scroll_ + row < entries.size(); ++row)
            {
                std::size_t      i        = scroll_ + row;
                const FileEntry& entry    = entries[i];
                bool             selected = static_cast<int>(i) == explorer_.selected();
                if (selected)
                    queue_.fill(254, 158 + row * 20, 668, 20, 0xC06018);
                uint32_t color = selected ? 0xFFFFFF : 0x202020;
                queue_.fill(262, 162 + row * 20, 12, 12, entry.directory ? 0x30C0F0 : 0x808080);
                if (entry.directory)
                    queue_.fill(264, 160 + row * 20, 6, 3, 0x30C0F0);
                else
                    queue_.fill(263, 163 + row * 20, 10, 10, 0xFFFFFF);
                queue_.text(280, 162 + row * 20, entry.name, color, 58);
                char size[32];
                std::sprintf(size, "%lu", static_cast<unsigned long>(entry.bytes));
                queue_.text(770, 162 + row * 20, entry.directory ? "Dossier" : size, color, 17);
            }
            queue_.button(78, 510, 64, "Prec.");
            queue_.button(150, 510, 68, "Suiv.");
            queue_.button(750, 510, 74, "Prec.");
            queue_.button(832, 510, 90, "Suiv.");
            queue_.text(
                78, 556, explorer_.mode() == FileExplorer::SelectFolder ? "Dossier :" : "Nom :");
            queue_.input(170, 548, 752, name_, focus_ == 2);
            queue_.button(78, 586, 140, "Nouveau dossier");
            queue_.button(650,
                          586,
                          138,
                          explorer_.confirming()                           ? "Remplacer"
                          : explorer_.mode() == FileExplorer::SaveFile     ? "Enregistrer"
                          : explorer_.mode() == FileExplorer::SelectFolder ? "Choisir"
                                                                           : "Ouvrir");
            queue_.button(800, 586, 122, "Annuler");
            queue_.text(78, 624, explorer_.message, 0x603020, 104);
        }

        int64_t FileDialog::paint(gui::Painter& painter)
        {
            if (!active_)
                return 0;
            if (queue_.finished() && dirty_)
                snapshot();
            return queue_.paint(painter);
        }
    }  // namespace desktop
}  // namespace gaspardos