# data file for the Fltk User Interface Designer (fluid)
version 1.0304
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-2020, 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 Library 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 <unistd.h>} {public global
}

decl {using namespace std;} {public global
}

decl {\#include "Params/Presets.h"} {public local
}

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

decl {extern SynthEngine *firstSynth;} {public local
}

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;
    p = NULL;
    make_window();
    copyW = 0;
    pasteW = 0;
    presetsDW = 265; // these are shared between both
    presetsDH = 430;} {}
  }
  Function {~PresetsUI()} {} {
    code {//
    copywin->hide();
    delete(copywin);
    pastewin->hide();
    delete(pastewin);} {}
  }
  Function {make_window()} {} {
    Fl_Window copywin {
      label {Copy to Clipboard/Preset}
      callback {saveWin(synth, o->w(), o->h(), o->x(), o->y(), false,"PresetWin");
o->hide();}
      xywh {190 173 265 430} type Double box PLASTIC_THIN_UP_BOX color 238 hide resizable
      code1 {o->size_range(265, 430, 0, 0, 0, 0, 1);} modal
    } {
      Fl_Browser copybrowse {
        callback {int val = o->value();
    if (val)
    {
      presetname->cut(0, presetname->maximum_size());
      presetname->insert(o->text(val));
    }}
        xywh {10 25 245 320} type Select
      }
      Fl_Button copypbutton {
        label {Copy to Preset}
        callback {//
        const char *tmp = presetname->value();
        if (tmp != NULL)
        {
        if (strlen(tmp) > 0)
        {
            p->copy(tmp);
            copywin->hide();
        }
        }}
        xywh {145 355 110 20} box THIN_UP_BOX
      }
      Fl_Button copybutton {
        label {Copy to Clipboard}
        callback {//
        p->copy(NULL);
        copywin->hide();}
        xywh {25 385 90 35} box THIN_UP_BOX align 192
      }
      Fl_Button copycancel {
        label Cancel
        callback {//
        copywin->hide();
        saveWin(synth, copywin->w(), copywin->h(), copywin->x(), copywin->y(), false,"PresetWin");}
        xywh {160 385 80 35} box THIN_UP_BOX align 192
      }
      Fl_Box copytype {
        label {Type:}
        xywh {10 5 40 15} labelsize 11 align 20
      }
      Fl_Box copytypetext {
        xywh {50 5 205 15} box FLAT_BOX color 238 labelfont 1 labelsize 11 align 20
      }
      Fl_Input presetname {
        callback {//
        const char *tmp = o->value();
        if (tmp == NULL)
        tmp = "";
        if (strlen(tmp) > 0)
        {
            copybutton->deactivate();
            copypbutton->activate();
        }
        else
        {
            copybutton->activate();
            copypbutton->deactivate();
        }}
        xywh {10 355 130 20} when 1
      }
    }
    Fl_Window pastewin {
      label {Paste from Clipboard/Preset}
      callback {saveWin(synth, o->w(), o->h(), o->x(), o->y(), false,"PresetWin");
o->hide();}
      xywh {463 173 265 430} type Double box PLASTIC_THIN_UP_BOX color 238 hide resizable
      code1 {o->size_range(265, 430, 0, 0, 0, 0, 1);} modal
    } {
      Fl_Browser pastebrowse {
        callback {//
        if (o->value() == 0)
        {
            pastepbutton->deactivate();
            deletepbutton->deactivate();
        }
        else
        {
            pastepbutton->activate();
            deletepbutton->activate();
        }}
        xywh {10 25 245 320} type Hold
      }
      Fl_Button pastepbutton {
        label {Paste from Preset}
        callback {//
        int n = pastebrowse->value();
        if (n == 0)
        {
            pastewin->hide();
            return;
        }
        synth->audioOut.store(_SYS_::mute::Request);
        while (synth->audioOut.load() == _SYS_::mute::Request)
            usleep (1000); // wait for full mute
        p->paste(n);
        pastewin->hide();
        pui->refresh();
        if (!synth->fileCompatible)
        {
            synth->getGuiMaster()->setmessage(UNUSED, true, "File from ZynAddSubFX 3.0 or later has parameter types changed incompatibly with earlier versions, and with Yoshimi. It may not perform correctly.");
            synth->fileCompatible = true;
            // this may not be necessary and anyway shouldn't
            // be done directly - trying to get rid of these!
        }
        if (synth->audioOut.load() == _SYS_::mute::Immediate)
            synth->audioOut.store(_SYS_::mute::Complete);}
        xywh {10 355 160 20} box THIN_UP_BOX
      }
      Fl_Button pastebutton {
        label {Paste from Clipboard}
        callback {//
        synth->audioOut.store(_SYS_::mute::Request);
        while (synth->audioOut.load() == _SYS_::mute::Request)
            usleep (1000); // wait for full mute
        p->paste(0);
        pastewin->hide();
        pui->refresh();
        if (synth->audioOut.load() == _SYS_::mute::Immediate)
            synth->audioOut.store(_SYS_::mute::Complete);}
        xywh {25 385 90 35} box THIN_UP_BOX align 192
      }
      Fl_Button pastecancel {
        label Cancel
        callback {//
        pastewin->hide();
        saveWin(synth, pastewin->w(), pastewin->h(), pastewin->x(), pastewin->y(), false,"PresetWin");}
        xywh {160 385 80 35} box THIN_UP_BOX align 192
      }
      Fl_Box pastetypetext {
        xywh {55 5 200 15} box FLAT_BOX color 238 labelfont 1 labelsize 11 align 20
      }
      Fl_Box pastetype {
        label {Type:}
        xywh {15 5 40 15} labelsize 11 align 20
      }
      Fl_Button deletepbutton {
        label Delete
        callback {//
        int n = pastebrowse->value();
        if (n != 0)
            p->deletepreset(n);
        rescan();}
        xywh {180 355 75 20} box THIN_UP_BOX
      }
    }
  }
  Function {copy(Presets *p)} {} {
    code {//
    copybutton->activate();
    copypbutton->deactivate();
    this->p = p;
    this->pui = NULL;
    bool but = (Fl::event_button() != FL_LEFT_MOUSE);
    presetname->cut(0, presetname->maximum_size());
    if (but)
        p->copy(NULL);
    else
    {
        rescan();
        copytypetext->label(&p->type[1]); // remove leading P
        copywin->show();
    }} {}
  }
  Function {paste(Presets *p,PresetsUI_ *pui)} {} {
    code {//
    this->p = p;
    this->pui = pui;
    bool but = (Fl::event_button() != FL_LEFT_MOUSE);
    pastepbutton->deactivate();
    deletepbutton->deactivate();
    if (but)
    {
        p->paste(0);
        pui->refresh();
    }
    else
    {
        rescan();
        pastetypetext->label(&p->type[1]); // remove leading P
        if (p->checkclipboardtype())
            pastebutton->activate();
        else
            pastebutton->deactivate();
        pastewin->show();
    }} {}
  }
  Function {copy(Presets *p,int n)} {} {
    code {//
    p->setelement(n);
    copy(p);} {}
  }
  Function {paste(Presets *p,PresetsUI_ *pui,int n)} {} {
    code {//
    p->setelement(n);
    paste(p, pui);} {}
  }
  Function {rescan()} {} {
    code {//
    copybrowse->clear();
    pastebrowse->clear();
    p->rescanforpresets(synth->getRuntime().currentPreset);
    for (int i = 0; i < MAX_PRESETS; ++i)
    {
        string name = firstSynth->getPresetsStore().presets[i].name;
        if (name.empty())
            break;
        copybrowse->add(name.c_str());
        pastebrowse->add(name.c_str());
    }
    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);

    copywin->resize(X, Y, W, H);
    pastewin->resize(X, Y, W, H);
    presetsRtext();} {}
  }
  Function {Hide()} {} {
    code {copywin->hide();} {}
  }
  Function {presetsRtext()} {} {
    code {//
    if (copywin->visible())
    {
        if (copyW != copywin->w())
        {
            copyW = copywin->w();
            pasteW = 0;

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

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

            copybrowse->textsize(size14);
            copypbutton->labelsize(size14);
            copybutton->labelsize(size14);
            copycancel->labelsize(size14);
            presetname->textsize(size14);
            copytype->labelsize(size11);
            copytypetext->labelsize(size11);

            copywin->redraw();
        }
    }
    else if (pastewin->visible())
    {
        if (pasteW != pastewin->w())
        {
            pasteW = pastewin->w();
            copyW = 0;

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

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

            pastebrowse->textsize(size14);
            pastepbutton->labelsize(size14);
            pastebutton->labelsize(size14);
            pastecancel->labelsize(size14);
            deletepbutton->labelsize(size14);
            pastetype->labelsize(size11);
            pastetypetext->labelsize(size11);

            pastewin->redraw();
        }
    }} {}
  }
  decl {Presets *p;} {public local
  }
  decl {PresetsUI_ *pui;} {public local
  }
  decl {SynthEngine *synth;} {private local
  }
  decl {int presetsDW;} {private local
  }
  decl {int presetsDH;} {private local
  }
  decl {int copyW;} {private local
  }
  decl {int pasteW;} {private local
  }
}
