# data file for the Fltk User Interface Designer (fluid)
version 1.0308
header_name {.h}
code_name {.cc}
comment {PresetsUI.h} {not_in_source in_header
}

comment {PresetsUI.cc} {in_source not_in_header
}

comment {Original ZynAddSubFX author Nasca Octavian Paul
Copyright (C) 2002-2005 Nasca Octavian Paul
Copyright 2009-2010, Alan Calvert
Copyright 2017-2023, Will Godfrey

This file is part of yoshimi, which is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.

yoshimi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.   See the GNU General Public License (version 2 or
later) for more details.

You should have received a copy of the GNU General Public License along with
yoshimi; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA  02110-1301, USA.

This file is a derivative of the ZynAddSubFX original.

} {selected in_source in_header
}

decl {\#include <FL/fl_ask.H>} {public local
}

decl {\#include <string>} {public global
}

decl {\#include <vector>} {public global
}

decl {\#include <unistd.h>} {public global
}

decl {\#include "Misc/TextMsgBuffer.h"

    namespace { // Implementation details...
        TextMsgBuffer& textMsgBuffer = TextMsgBuffer::instance();
    }} {private local
}

decl {\#include "Misc/SynthEngine.h"} {public global
}

decl {\#include "MasterUI.h"} {private global
}

class PresetsUI_ {} {
  Function {refresh()} {return_type {virtual void}
  } {
    code {//} {}
  }
  Function {~PresetsUI_()} {} {
    code {//
    ;} {}
  }
}

class PresetsUI {} {
  Function {PresetsUI(SynthEngine *_synth)} {} {
    code {//
    synth = _synth;
    make_window();
    UcopyW = 0;
    UpasteW = 0;
    presetsDW = 265; // these are shared between both
    presetsDH = 430;} {}
  }
  Function {~PresetsUI()} {} {
    code {//
     if (Upastewin->visible())
         saveWin(synth, Upastewin->w(), Upastewin->h(), Upastewin->x(), Upastewin->y(), false,"PresetWin");
     else if (Ucopywin->visible())
           saveWin(synth, Ucopywin->w(), Ucopywin->h(), Ucopywin->x(), Ucopywin->y(), false,"PresetWin");

    Ucopywin->hide();
    delete(Ucopywin);
    Upastewin->hide();
    delete(Upastewin);
    ;} {}
  }
  Function {make_window()} {} {
    Fl_Window Ucopywin {
      label {Yoshimi : Copy to}
      callback {saveWin(synth, o->w(), o->h(), o->x(), o->y(), false,"PresetWin");
o->hide();}
      xywh {200 183 265 430} type Double box PLASTIC_THIN_UP_BOX color 7 hide resizable
      code1 {o->size_range(265, 430, 0, 0, 0, 0, 1);} modal
    } {
      Fl_Browser Ucopybrowse {
        callback {//

            int result = o->value();
            Upresetname->cut(0, 25);
            Upresetname->insert(o->text(result));
            Upresetname->position(0);}
        xywh {10 25 245 320} type Select labelcolor 64 textcolor 64
      }
      Fl_Button UcopyPresbutton {
        label {Copy to Preset}
        callback {// to file

            string name = string(Upresetname->value());
            int miscMsg = textMsgBuffer.push(name);
            send_data(0, TOPLEVEL::action::lowPrio, TOPLEVEL::type::Copy, TOPLEVEL::control::copyPaste, npart, kititem, engine, insert, parameter, offset, miscMsg);
            Ucopywin->hide();}
        xywh {145 355 110 24} box PLASTIC_THIN_UP_BOX color 228 labelcolor 64
      }
      Fl_Button UcopyClipbutton {
        label {Copy to Clipboard}
        callback {// to clipboard

        send_data(0, TOPLEVEL::action::forceUpdate | TOPLEVEL::action::lowPrio, TOPLEVEL::type::Copy, TOPLEVEL::control::copyPaste, npart, kititem, engine, insert, parameter, offset, UNUSED);
        Ucopywin->hide();}
        xywh {25 385 90 35} box PLASTIC_THIN_UP_BOX color 228 labelcolor 64 align 192
      }
      Fl_Button Ucopycancel {
        label Cancel
        callback {//

       Ucopywin->hide();
       saveWin(synth, Ucopywin->w(), Ucopywin->h(), Ucopywin->x(), Ucopywin->y(), false,"PresetWin");}
        xywh {160 385 80 35} box THIN_UP_BOX color 196 labelcolor 64 align 192
      }
      Fl_Box Ucopytype {
        label {Type:}
        xywh {10 5 40 15} labelsize 11 labelcolor 64 align 20
      }
      Fl_Box Ucopytypetext {
        xywh {50 5 205 15} box FLAT_BOX color 7 labelfont 1 labelsize 11 labelcolor 64 align 20
        code0 {o->label(groupName.c_str());}
      }
      Fl_Input Upresetname {
        callback {//

        const char *tmp = o->value();
        if (tmp == NULL)
        tmp = "";
        if (strlen(tmp) > 0)
        {
            UcopyPresbutton->activate();
        }
        else
        {
            UcopyPresbutton->deactivate();
        }}
        xywh {10 355 130 24} labelcolor 64 when 1 textcolor 64
        code0 {o->maximum_size(25);}
      }
    }
    Fl_Window Upastewin {
      label Yoshimi
      callback {saveWin(synth, o->w(), o->h(), o->x(), o->y(), false,"PresetWin");
o->hide();}
      xywh {223 91 265 430} type Double box PLASTIC_THIN_UP_BOX color 7 hide resizable
      code1 {o->size_range(265, 430, 0, 0, 0, 0, 1);} modal
    } {
      Fl_Browser Upastebrowse {
        callback {//

        if (o->value())
        {
            int result = o->value();
            if (result > 0)
            {
                UpastePresbutton->activate();
                Udeletepbutton->activate();
            }
        }
        else
        {
            UpastePresbutton->deactivate();
            Udeletepbutton->deactivate();
        }}
        xywh {10 25 245 320} type Hold labelcolor 64 textcolor 64
      }
      Fl_Button UpastePresbutton {
        label {Paste from Preset}
        callback {// from preset

            if (Upastebrowse->value() <= 0)
            return;

            string name = Upastebrowse->text(Upastebrowse->value());
            int miscMsg = textMsgBuffer.push(name);
            send_data(0, TOPLEVEL::action::forceUpdate | TOPLEVEL::action::lowPrio, TOPLEVEL::type::Paste, TOPLEVEL::control::copyPaste, npart, kititem, engine, insert, parameter, offset, miscMsg);
            Upastewin->hide();}
        xywh {10 355 160 24} box PLASTIC_THIN_UP_BOX color 228 labelcolor 64
      }
      Fl_Button UpasteClipbutton {
        label {Paste from Clipboard}
        callback {// from clipboard

          send_data(0, TOPLEVEL::action::forceUpdate | TOPLEVEL::action::lowPrio, TOPLEVEL::type::Paste, TOPLEVEL::control::copyPaste, npart, kititem, engine, insert, parameter, offset, UNUSED);
          Upastewin->hide();}
        xywh {25 385 90 35} box PLASTIC_THIN_UP_BOX color 228 labelcolor 64 align 192
      }
      Fl_Button Upastecancel {
        label Cancel
        callback {//

        Upastewin->hide();
        saveWin(synth, Upastewin->w(), Upastewin->h(), Upastewin->x(), Upastewin->y(), false,"PresetWin");}
        xywh {160 385 80 35} box THIN_UP_BOX color 196 labelcolor 64 align 192
      }
      Fl_Box Upastetypetext {
        xywh {55 5 200 15} box FLAT_BOX color 7 labelfont 1 labelsize 11 labelcolor 64 align 20
        code0 {o->label(groupName.c_str());}
      }
      Fl_Box Upastetype {
        label {Type:}
        xywh {15 5 40 15} labelsize 11 labelcolor 64 align 20
      }
      Fl_Button Udeletepbutton {
        label Delete
        callback {//
string name = Upastebrowse->text(Upastebrowse->value());
int miscMsg = textMsgBuffer.push(name);
send_data(1, TOPLEVEL::action::lowPrio, TOPLEVEL::type::Paste, TOPLEVEL::control::copyPaste, npart, kititem, engine, insert, parameter, offset, miscMsg);
Upastewin->hide();}
        xywh {180 355 75 24} box PLASTIC_THIN_UP_BOX color 228 labelcolor 64
      }
    }
  }
  Function {setExten()} {return_type string
  } {
    code {//
    string text = "";
    if (npart == TOPLEVEL::systemEffects)
        text = "system";
    else if (npart == TOPLEVEL::insertEffects)
        text = "insert";
    else
        text = "part " + std::to_string(npart + 1);
    return text;} {}
  }
  Function {Ucopy(int _npart, int _kititem, int _engine = UNUSED, int _insert =  UNUSED, int _parameter = UNUSED, int _offset = UNUSED)} {} {
    code {//

    npart = _npart;
    kititem = _kititem;
    engine = _engine;
    insert = _insert;
    parameter = _parameter;
    offset = _offset;
    Ucopywin->copy_label(textMsgBuffer.fetch(collect_readData(synth, textMsgBuffer.push("Copy from - " + setExten()), UNUSED, TOPLEVEL::windowTitle)).c_str());
    string names = Ufetch();
    if (names.empty())
        return;

    size_t pos = 0;
    unsigned char ch = 10;
    Ucopybrowse->clear();
    UcopyPresbutton->deactivate();
    Ucopytypetext->label(groupName.c_str());
    do {
        pos = names.find(ch);
        if (pos != string::npos)
        {
            string line = names.substr(0, pos);
            names = names.substr(pos + 1, names.length());
            Ucopybrowse->add(line.c_str());
        }
        else
            Ucopybrowse->add(names.c_str());
    }
    while(pos != string::npos);

    Ucopywin->show();} {}
  }
  Function {Upaste(int _npart, int _kititem, int _engine = UNUSED, int _insert =  UNUSED, int _parameter = UNUSED, int _offset = UNUSED)} {} {
    code {//

    npart = _npart;
    kititem = _kititem;
    engine = _engine;
    insert = _insert;
    parameter = _parameter;
    offset = _offset;
    Upastewin->copy_label(textMsgBuffer.fetch(collect_readData(synth, textMsgBuffer.push("Paste to - " + setExten()), UNUSED, TOPLEVEL::windowTitle)).c_str());
    string names = Ufetch();
    if (names.empty())
        return;
    size_t pos = 0;
    unsigned char ch = 10;
    Upastebrowse->clear();
    UpastePresbutton->deactivate();
    UpasteClipbutton->activate();
    Udeletepbutton->deactivate();
    Upastetypetext->label(groupName.c_str());
    do {
        pos = names.find(ch);
        if (pos != string::npos)
        {
            string line = names.substr(0, pos);
            names = names.substr(pos + 1, names.length());
            Upastebrowse->add(line.c_str());
        }
        else
            Upastebrowse->add(names.c_str());
    }
    while(pos != string::npos);
    if (Ufetch(2).empty())
        UpasteClipbutton->deactivate();
    else
        UpasteClipbutton->activate();
    Upastewin->show();} {}
  }
  Function {Ufetch(int sent = 0)} {return_type string
  } {
    code {//
    int  W, H, X, Y, O;
    loadWin(synth, W, H, X, Y, O, "PresetWin");
    if (W < presetsDW || H < presetsDH)
    {
        W = presetsDW;
        H = presetsDH;
    }
    checkSane(X, Y, W, H, presetsDW, presetsDH, true);

    Ucopywin->resize(X, Y, W, H);
    Upastewin->resize(X, Y, W, H);
    UpresetsRtext();

    CommandBlock putData;
    putData.data.value = sent;
    putData.data.type = TOPLEVEL::type::List;
    putData.data.source = TOPLEVEL::action::lowPrio;
    putData.data.control = TOPLEVEL::control::copyPaste;
    putData.data.part = npart;
    putData.data.kit = kititem;
    putData.data.engine = engine;
    putData.data.insert = insert;
    putData.data.parameter = parameter;
    putData.data.offset = offset;
    putData.data.miscmsg = UNUSED;
    //synth->CBtest(&putData);
    int value = synth->interchange.readAllData(putData);
    if (sent == 2)
    {
        return textMsgBuffer.fetch(value);
    }
    putData.data.value = 1;
    int Ugroup = synth->interchange.readAllData(putData);
    groupName = textMsgBuffer.fetch(Ugroup);
    //std::cout << "In GUI >" << groupName << std::endl;
    return textMsgBuffer.fetch(value);} {}
  }
  Function {send_data(float value, int action, int type, int control, int part = UNUSED, int kititem = UNUSED, int engine = UNUSED, int insert = UNUSED, int parameter = UNUSED, int offset = UNUSED, int miscmsg = UNUSED)} {} {
    code {//
        collect_writeData(synth, value, action, type, control, part, kititem, engine, insert, parameter, offset, miscmsg);} {}
  }
  Function {UpresetsRtext()} {} {
    code {//

    if (Ucopywin->visible())
    {
        if (UcopyW != Ucopywin->w())
        {
            UcopyW = Ucopywin->w();
            UpasteW = 0;

            float dScale = Ucopywin->w() / float(presetsDW);

            int size14 = int(14 * dScale);
            int size11 = int(11 * dScale);

            Ucopybrowse->textsize(size14);
            UcopyPresbutton->labelsize(size14);
            UcopyClipbutton->labelsize(size14);
            Ucopycancel->labelsize(size14);
            Upresetname->textsize(size14);
            Ucopytype->labelsize(size11);
            Ucopytypetext->labelsize(size11);

            Ucopywin->redraw();
        }
    }
    else if (Upastewin->visible())
    {
        if (UpasteW != Upastewin->w())
        {
            UpasteW = Upastewin->w();
            UcopyW = 0;

            float dScale = Upastewin->w() / float(presetsDW);

            int size14 = int(14 * dScale);
            int size11 = int(11 * dScale);

            Upastebrowse->textsize(size14);
            UpastePresbutton->labelsize(size14);
            UpasteClipbutton->labelsize(size14);
            Upastecancel->labelsize(size14);
            Udeletepbutton->labelsize(size14);
            Upastetype->labelsize(size11);
            Upastetypetext->labelsize(size11);

            Upastewin->redraw();
        }

    } {}} {}
  }
  decl {SynthEngine *synth;} {private local
  }
  decl {int presetsDW;} {private local
  }
  decl {int presetsDH;} {private local
  }
  decl {int UcopyW;} {private local
  }
  decl {int UpasteW;} {private local
  }
  decl {int npart;} {private local
  }
  decl {int kititem;} {private local
  }
  decl {int engine;} {private local
  }
  decl {int insert;} {private local
  }
  decl {int offset;} {private local
  }
  decl {int parameter;} {private local
  }
  decl {string groupName;} {private local
  }
}
