/*                                                         -*- c++ -*-
 * Copyright (C) 2006-2019 Andreas Persson
 * Copyright (C) 2007-2026 Christian Schoenebeck
 *
 * This program 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, or (at
 * your option) any later version.
 *
 * This program 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with program; see the file COPYING. If not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

#ifndef GIGEDIT_PARAMEDIT_H
#define GIGEDIT_PARAMEDIT_H

#ifdef LIBGIG_HEADER_FILE
# include LIBGIG_HEADER_FILE(gig.h)
#else
# include <gig.h>
#endif

#include <cmath>

#include "compat.h"
#include "GEColor.h"

#include <glibmm/convert.h>
#include <gtkmm/box.h>
#include <gtkmm/adjustment.h>
#if HAS_GTKMM_ALIGNMENT
# include <gtkmm/alignment.h>
#endif
#include <gtkmm/checkbutton.h>
#include <gtkmm/comboboxtext.h>
#include <gtkmm/frame.h>
#include <gtkmm/label.h>
#include <gtkmm/scale.h>
#include <gtkmm/spinbutton.h>
#if USE_GTKMM_GRID
# include <gtkmm/grid.h>
#else
# include <gtkmm/table.h>
#endif
#include <gtkmm/textview.h>
#include <gtkmm/liststore.h>
#include <gtkmm/eventbox.h>

#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 12) || GTKMM_MAJOR_VERSION < 2
#define OLD_TOOLTIPS
#include <gtkmm/tooltips.h>
#endif

int note_value(const Glib::ustring& note);
std::string note_str(uint8_t note);

void spin_button_show_notes(Gtk::SpinButton& spin_button);

/** @brief Interface class for all property widgets in Gigedit.
 *
 * Gigedit uses numerous custom widget classes that are used for changing sound
 * parameters, instrument patch settings, sample settings and so forth. This
 * class defines a common interface that allows Gigedit to control all of them
 * independent from which other base class (e.g. some stock Gtk widget base)
 * that custom widget class might be derived from or not.
 */
class GEPropWidgetAPI {
    Glib::ustring m_shortDescr;
public:
    /** @brief Describes in short the purpose of this sound parameter.
     *
     * This is used to be displayed in the UI of what kind of sound parameter
     * this property control widget manages.
     */
    virtual Glib::ustring shortDescription() const { return m_shortDescr; }

    /** @brief Set (or override) short description.
     *
     * Many widgets have their built-in descript that they return automatically.
     * By calling this method that description can be overridden though. E.g.
     * to fine tune what's going to be displayed for this particular parameter.
     */
    virtual void setShortDescription(const Glib::ustring& s) { m_shortDescr = s; }

    /** @brief Signal alternative values state has changed.
     *
     * This signal is emitted if set_alt_value(bool) was called with a different
     * bool value than the current one.
     */
    sigc::signal<void,bool> signal_alt_value_changed;

    /** @brief Indicate that displayed property has alternative values.
     *
     * Called to inform the widget whether there are multiple (i.e. different)
     * values for the parameter displayed by this widget. Widget should update
     * visualization by highlighting the parameter value if there are multiple
     * values for this parameter.
     *
     * @param alt - true if there are multiple values for parameter
     */
    virtual void set_alt_value(bool alt) = 0;

    /** @brief Current knowledge of widget whether thare are alternative values.
     *
     * Returns true if widget currently highlights its parameter value due to
     * being in knowledge that there were multiple (i.e. different) values for
     * the parameter displayed by widget.
     */
    virtual bool has_alt_value() const = 0;

    /** @brief Actual Gtk widget that handles keyboard / mouse events.
     *
     * Returns the Gtk widget that actually controls changing the parameter for
     * this property by the user (via mouse / keyboard).
     *
     * Most property widgets are an aggregate of different Gtk widgets. This
     * method returns the actual Gtk widget that handles keyboard / mouse
     * events. The correct Gtk widget object is needed for successfully
     * registering event handlers for mouse enter / leave events for instance,
     * which are simply silently ignored by any other Gtk widget object.
     *
     * Note: for some Gtk classes like Gtk::ComboBox this still does not work,
     * as it is an aggregate as well, and it is not obvious which one of its
     * child object is the event controlling widget. The only way to address
     * this is to wrap the Gtk::ComboBox into a Gtk::EventBox. That's a widget
     * itself though, so it changes the way the combo boxes are packed / layout.
     *
     * In Gtkmm 4 the solution would be to use Gtk::EventControllerMotion
     * instead, which is a separate object that does not influence widget
     * packing / layout.
     */
    virtual Gtk::Widget* propControlWidget() = 0;
};

/** @brief Customized variant of Gtk::ComboBoxText
 *
 * Provides similar methods as Gtk::ComboBoxText, additionally provides
 * set_alt_value(bool) to highlight alternating value in color.
 *
 * This is deriving Gtk::ComboBox instead of Gtk::ComboBoxText, because the
 * former does not have a cell renderer on construction, the latter does, which
 * would create multiple issues (e.g. warnings of multiple registration of
 * property assignments).
 */
class GEComboBoxText : public Gtk::ComboBox, public GEPropWidgetAPI {
    int altIndex;
public:
    class Columns : public Gtk::TreeModel::ColumnRecord {
    public:
        Gtk::TreeModelColumn<Glib::ustring> text;
        Gtk::TreeModelColumn<Glib::ustring> color;

        Columns() {
            add(text);
            add(color);
        }
    };

    GEComboBoxText();
    void append(const Glib::ustring& text);
    void remove_all();
    void set_alt_value(bool alt) override;
    bool has_alt_value() const override;
    Gtk::Widget* propControlWidget() override { return this; }

    Columns columns;
    Glib::RefPtr<Gtk::ListStore> model;
};

// abstract class (i.e. cannot be instantiated directly)
class LabelWidget : public GEPropWidgetAPI {
public:
    Gtk::Label label;
    Gtk::Widget& widget;

    static const int verticalMargin = 5;

    void set_sensitive(bool sensitive = true);
    Glib::ustring shortDescription() const override;
    void set_alt_value(bool alt) override;
    bool has_alt_value() const override;
    sigc::signal<void>& signal_value_changed() {
        return sig_changed;
    }
protected:
    LabelWidget(const char* labelText, Gtk::Widget& widget);
    static void apply_alt_value_color(bool alt, Gtk::Widget* widget);
    virtual void on_show_tooltips_changed();
#ifdef OLD_TOOLTIPS
    Gtk::Tooltips tooltips;
#endif
    sigc::signal<void> sig_changed;
    bool m_alt;
};

class ReadOnlyLabelWidget : public LabelWidget {
public:
    Gtk::EventBox eventbox;
    Gtk::Label text;

    ReadOnlyLabelWidget(const char* leftHandText);
    ReadOnlyLabelWidget(const char* leftHandText, const char* rightHandText);
    Gtk::Widget* propControlWidget() override { return &eventbox; }
};

template<int N>
class PartitonedReadOnlyLabelWidget : public LabelWidget {
    Gtk::EventBox eventbox;
    HBox hbox;
public:
    class Partition : public GEPropWidgetAPI {
    protected:
        PartitonedReadOnlyLabelWidget<N>* parent;
        bool alt = false;
        Gtk::EventBox eventbox;
        friend class PartitonedReadOnlyLabelWidget<N>;
    public:
        Gtk::Label text;

        Partition() {
            #if HAS_GTKMM_ALIGNMENT
            text.set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_START);
            #else
            text.set_halign(Gtk::Align::START);
            text.set_valign(Gtk::Align::START);
            #endif
            eventbox.add(text);
        }
        Glib::ustring shortDescription() const override {
            if (!GEPropWidgetAPI::shortDescription().empty())
                return GEPropWidgetAPI::shortDescription();
            return parent->label.get_text();
        }
        void set_alt_value(bool alt) override {
            if (this->alt == alt) return;
            this->alt = alt;
            LabelWidget::apply_alt_value_color(alt, &text);
            signal_alt_value_changed.emit(alt);
        }
        bool has_alt_value() const override {
            return alt;
        }
        Gtk::Widget* propControlWidget() override { return &eventbox; }
    };

    Partition part[N];

    PartitonedReadOnlyLabelWidget(const char* leftHandText) :
        LabelWidget(leftHandText, eventbox)
    {
        for (int i = 0; i < N; ++i) {
            part[i].parent = this;
            part[i].signal_alt_value_changed.connect(
                signal_alt_value_changed.make_slot()
            );
            hbox.pack_start(part[i].eventbox, Gtk::PACK_SHRINK);
        }
        eventbox.add(hbox);
        // on Gtk 3 there is absolutely no margin by default
        #if GTKMM_MAJOR_VERSION >= 3
        eventbox.set_margin_top( LabelWidget::verticalMargin );
        eventbox.set_margin_bottom( LabelWidget::verticalMargin );
        #endif
    }
    Gtk::Widget* propControlWidget() override { return &eventbox; }
};

class NumEntry : public LabelWidget {
protected:
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
    Gtk::Adjustment adjust;
#else
    Glib::RefPtr<Gtk::Adjustment> adjust;
#endif
    HScale scale;
    Gtk::SpinButton spinbutton;
    HBox box;

    static int round_to_int(double x) {
        return int(x < 0.0 ? x - 0.5 : x + 0.5);
    }

public:
    NumEntry(const char* labelText, double lower = 0, double upper = 127,
             int decimals = 0);
    void set_tip(const Glib::ustring& tip_text) {
#ifdef OLD_TOOLTIPS
        tooltips.set_tip(spinbutton, tip_text);
#else
        spinbutton.set_tooltip_text(tip_text);
#endif
    }
    void set_upper(double upper) {
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
        adjust.set_upper(upper);
#else
        adjust->set_upper(upper);
#endif
    }
    void set_increments(double step, double page) {
        spinbutton.set_increments(step, page);
    }
    void set_alt_value(bool alt) override;
    bool has_alt_value() const override;
    Gtk::Widget* propControlWidget() override { return &spinbutton; }
    void on_show_tooltips_changed();
};

class NumEntryGain : public NumEntry {
private:
    int32_t value;
    void value_changed();
    double coeff;
    bool connected;
public:
    NumEntryGain(const char* labelText,
                 double lower, double upper, int decimals, double coeff);
    int32_t get_value() const { return value; }
    void set_value(int32_t value);
    std::string gainToStr(int32_t value) const;
};

template<typename T>
class NumEntryTemp : public NumEntry {
private:
    T value;
    void value_changed();
public:
    NumEntryTemp(const char* labelText,
                 double lower = 0, double upper = 127, int decimals = 0);
    T get_value() const { return value; }
    void set_value(T value);
};

template<typename T>
NumEntryTemp<T>::NumEntryTemp(const char* labelText,
                              double lower, double upper, int decimals) :
    NumEntry(labelText, lower, upper, decimals),
    value(0)
{
    spinbutton.signal_value_changed().connect(
        sigc::mem_fun(*this, &NumEntryTemp::value_changed));
}

template<typename T>
void NumEntryTemp<T>::value_changed()
{
    const double f = pow(10, spinbutton.get_digits());
    int new_value = round_to_int(spinbutton.get_value() * f);
    if (new_value != round_to_int(value * f)) {
        value = T(new_value / f);
        sig_changed();
    }
}

template<typename T>
void NumEntryTemp<T>::set_value(T value)
{
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
    if (value > adjust.get_upper()) value = T(adjust.get_upper());
#else
    if (value > adjust->get_upper()) value = T(adjust->get_upper());
#endif
    if (this->value != value) {
        this->value = value;
        const double f = pow(10, spinbutton.get_digits());
        if (round_to_int(spinbutton.get_value() * f) != round_to_int(value * f)) {
            spinbutton.set_value(value);
        }
        sig_changed();
    }
}


class NoteEntry : public NumEntryTemp<uint8_t> {
public:
    NoteEntry(const char* labelText);
private:
    int on_input(double* new_value);
    bool on_output();
};


class NumEntryPermille : public NumEntry {
private:
    uint16_t value;
    void value_changed();
public:
    NumEntryPermille(const char* labelText,
                     double lower = 0, double upper = 127, int decimals = 0);
    uint16_t get_value() const { return value; }
    void set_value(uint16_t value);
};

class ChoiceEntryBase : public LabelWidget {
protected:
    ChoiceEntryBase(const char* labelText, Gtk::Widget& widget);
    Gtk::EventBox eventbox;
    GEComboBoxText combobox;
    void on_show_tooltips_changed();
public:
    void set_alt_value(bool alt) override;
    bool has_alt_value() const override;
    Gtk::Widget* propControlWidget() override { return &eventbox; }
};

template<typename T>
class ChoiceEntry : public ChoiceEntryBase {
private:
#if HAS_GTKMM_ALIGNMENT
    Gtk::Alignment align;
#endif
    const T* values;
public:
    ChoiceEntry(const char* labelText);
    T get_value() const;
    void set_value(T value);
    void set_choices(const char** texts, const T* values);

    void set_tip(const Glib::ustring& tip_text) {
#ifdef OLD_TOOLTIPS
        tooltips.set_tip(combobox, tip_text);
#else
        combobox.set_tooltip_text(tip_text);
#endif
    }
};

template<typename T>
ChoiceEntry<T>::ChoiceEntry(const char* labelText) :
#if HAS_GTKMM_ALIGNMENT
    ChoiceEntryBase(labelText, align),
    align(0, 0, 0, 0),
#else
    ChoiceEntryBase(labelText, eventbox),
#endif
    values(0)
{
    combobox.signal_changed().connect(sig_changed.make_slot());
#if HAS_GTKMM_ALIGNMENT
    align.add(eventbox);
#endif
}

template<typename T>
void ChoiceEntry<T>::set_choices(const char** texts, const T* values)
{
    combobox.remove_all();
    for (int i = 0 ; texts[i] ; i++) {
        combobox.append(texts[i]);
    }
    this->values = values;
}

template<typename T>
T ChoiceEntry<T>::get_value() const
{
    int rowno = combobox.get_active_row_number();
    return values[rowno];
}

template<typename T>
void ChoiceEntry<T>::set_value(T value)
{
    int row = 0;
    int nb_rows = combobox.get_model()->children().size();
    for (; row < nb_rows ; row++) {
        if (value == values[row]) break;
    }
    combobox.set_active(row == nb_rows ? -1 : row);
}


class ChoiceEntryLeverageCtrl : public LabelWidget {
private:
    gig::leverage_ctrl_t value;
    Gtk::EventBox eventbox;
    GEComboBoxText combobox;
#if HAS_GTKMM_ALIGNMENT
    Gtk::Alignment align;
#endif
    void value_changed();
protected:
    void on_show_tooltips_changed();
public:
    ChoiceEntryLeverageCtrl(const char* labelText);
    gig::leverage_ctrl_t get_value() const { return value; }
    void set_value(gig::leverage_ctrl_t value);
    void set_alt_value(bool alt) override;
    bool has_alt_value() const override;
    Gtk::Widget* propControlWidget() override { return &eventbox; }
    void set_tip(const Glib::ustring& tip_text) {
        combobox.set_tooltip_text(tip_text);
    }
};

class ChoiceEntryLfoWave : public LabelWidget {
private:
    gig::lfo_wave_t value;
    Gtk::EventBox eventbox;
    GEComboBoxText combobox;
#if HAS_GTKMM_ALIGNMENT
    Gtk::Alignment align;
#endif
    void value_changed();
protected:
    void on_show_tooltips_changed();
public:
    ChoiceEntryLfoWave(const char* labelText);
    gig::lfo_wave_t get_value() const { return value; }
    void set_value(gig::lfo_wave_t value);
    void set_alt_value(bool alt) override;
    bool has_alt_value() const override;
    Gtk::Widget* propControlWidget() override { return &eventbox; }
    void set_tip(const Glib::ustring& tip_text) {
        combobox.set_tooltip_text(tip_text);
    }
};


class BoolEntry : public LabelWidget {
private:
    Gtk::CheckButton checkbutton;
public:
    BoolEntry(const char* labelText);
    bool get_value() const { return checkbutton.get_active(); }
    void set_value(bool value) { checkbutton.set_active(value); }
    Gtk::Widget* propControlWidget() override { return &checkbutton; }

    void set_tip(const Glib::ustring& tip_text) {
#ifdef OLD_TOOLTIPS
        tooltips.set_tip(checkbutton, tip_text);
#else
        checkbutton.set_tooltip_text(tip_text);
#endif
    }
};

class BoolBox : public Gtk::CheckButton, public GEPropWidgetAPI {
public:
    BoolBox(const char* labelText);
    bool get_value() const { return get_active(); }
    void set_value(bool value) { set_active(value); }
    void set_alt_value(bool alt) override;
    bool has_alt_value() const override;
    Gtk::Widget* propControlWidget() override { return this; }
    sigc::signal<void>& signal_value_changed() { return sig_changed; }
protected:
    void on_show_tooltips_changed();

    sigc::signal<void> sig_changed;
    bool m_alt;
};


class StringEntry : public LabelWidget {
private:
    Gtk::Entry entry;
public:
    StringEntry(const char* labelText);
    gig::String get_value() const;
    void set_value(const gig::String& value);
    void set_width_chars(int n_chars) { entry.set_width_chars(n_chars); }
    Gtk::Widget* propControlWidget() override { return &entry; }
};

class StringEntryMultiLine : public LabelWidget {
private:
    Gtk::TextView text_view;
    Glib::RefPtr<Gtk::TextBuffer> text_buffer;
    Gtk::Frame frame;
protected:
    void on_show_tooltips_changed();
public:
    StringEntryMultiLine(const char* labelText);
    gig::String get_value() const;
    void set_value(const gig::String& value);
    Gtk::Widget* propControlWidget() override { return &text_view; }
};


/**
 * Container widget for LabelWidgets.
 */
class Table :
#if USE_GTKMM_GRID
    public Gtk::Grid
#else
    public Gtk::Table
#endif
{
public:
    Table(int x, int y);
    void add(BoolEntry& boolentry);
    void add(LabelWidget& labelwidget);
private:
#if USE_GTKMM_GRID
    int cols;
#endif
    int rowno;
};


/**
 * Base class for editor components that use LabelWidgets to edit
 * member variables of the same class. By connecting the widgets to
 * members of the model class, the model is automatically kept
 * updated.
 */
template<class M>
class PropEditor {
public:
    sigc::signal<void>& signal_changed() {
        return sig_changed;
    }
protected:
    M* m;
    int update_model; // to prevent infinite update loops
    PropEditor() : m(0), update_model(0) { }
    sigc::signal<void> sig_changed;

    template<class C, typename T>
    void connect(C& widget, T M::* member) {
        // gcc 4.1.2 needs this temporary variable to resolve the
        // address
        void (PropEditor::*f)(const C* w, T M::* member) =
            &PropEditor::set_member;
        widget.signal_value_changed().connect(
            sigc::bind(sigc::mem_fun(*this, f), &widget, member));

        void (PropEditor::*g)(C* w, T M::* member) =
            &PropEditor::get_member;
        sig.connect(
            sigc::bind(sigc::mem_fun(*this, g), &widget, member));
    }

    template<class C, class S, typename T>
    void connect(C& widget, void (S::*setter)(T)) {
        void (PropEditor::*f)(const C* w, void (S::*setter)(T)) =
            &PropEditor<M>::call_setter;
        widget.signal_value_changed().connect(
            sigc::bind(sigc::mem_fun(*this, f), &widget, setter));
    }

    template<class C, class F>
    void connectLambda(C& widget, F fn) {
        widget.signal_value_changed().connect([&widget,fn]{
            fn( widget.get_value() );
        });
    }

    void connect(NoteEntry& eKeyRangeLow, NoteEntry& eKeyRangeHigh,
                 gig::range_t M::* range) {
        eKeyRangeLow.signal_value_changed().connect(
            sigc::bind(
                sigc::mem_fun(*this, &PropEditor::key_range_low_changed),
                &eKeyRangeLow, &eKeyRangeHigh, range));
        eKeyRangeHigh.signal_value_changed().connect(
            sigc::bind(
                sigc::mem_fun(*this, &PropEditor::key_range_high_changed),
                &eKeyRangeLow, &eKeyRangeHigh, range));
        sig.connect(
            sigc::bind(sigc::mem_fun(*this, &PropEditor::get_key_range),
                       &eKeyRangeLow, &eKeyRangeHigh, range));
    }

    void update(M* m) {
        update_model++;
        this->m = m;
        sig.emit();
        update_model--;
    }

private:
    sigc::signal<void> sig;

    void key_range_low_changed(NoteEntry* eKeyRangeLow,
                               NoteEntry* eKeyRangeHigh,
                               gig::range_t M::* range) {
        if (update_model == 0) {
            uint8_t value = eKeyRangeLow->get_value();
            (m->*range).low = value;
            if (value > (m->*range).high) {
                eKeyRangeHigh->set_value(value);
            }
            sig_changed();
        }
    }

    void key_range_high_changed(NoteEntry* eKeyRangeLow,
                                NoteEntry* eKeyRangeHigh,
                                gig::range_t M::* range) {
        if (update_model == 0) {
            uint8_t value = eKeyRangeHigh->get_value();
            (m->*range).high = value;
            if (value < (m->*range).low) {
                eKeyRangeLow->set_value(value);
            }
            sig_changed();
        }
    }

    template<class C, typename T>
    void set_member(const C* w, T M::* member) {
        if (update_model == 0) {
            m->*member = w->get_value();
            sig_changed();
        }
    }

    template<class C, typename T>
    void get_member(C* w, T M::* member) {
        w->set_value(m->*member);
    }

    void get_key_range(NoteEntry* eKeyRangeLow,
                       NoteEntry* eKeyRangeHigh,
                       gig::range_t M::* range) const {
        eKeyRangeLow->set_value((m->*range).low);
        eKeyRangeHigh->set_value((m->*range).high);
    }

    template<class C, class S, typename T>
    void call_setter(const C* w, void (S::*setter)(T)) {
        if (update_model == 0) {
            (static_cast<S*>(this)->*setter)(w->get_value());
            sig_changed();
        }
    }
};

#endif
