/[svn]/gigedit/trunk/src/gigedit/paramedit.h
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/paramedit.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1261 by persson, Thu Jul 5 17:12:20 2007 UTC revision 3329 by schoenebeck, Sun Jul 23 18:31:53 2017 UTC
# Line 1  Line 1 
1  /*                                                         -*- c++ -*-  /*                                                         -*- c++ -*-
2   * Copyright (C) 2006, 2007 Andreas Persson   * Copyright (C) 2006-2017 Andreas Persson
3   *   *
4   * This program is free software; you can redistribute it and/or   * This program is free software; you can redistribute it and/or
5   * modify it under the terms of the GNU General Public License as   * modify it under the terms of the GNU General Public License as
# Line 20  Line 20 
20  #ifndef GIGEDIT_PARAMEDIT_H  #ifndef GIGEDIT_PARAMEDIT_H
21  #define GIGEDIT_PARAMEDIT_H  #define GIGEDIT_PARAMEDIT_H
22    
23  #include <gig.h>  #ifdef LIBGIG_HEADER_FILE
24    # include LIBGIG_HEADER_FILE(gig.h)
25    #else
26    # include <gig.h>
27    #endif
28    
29    #include <cmath>
30    
31    #include <glibmm/convert.h>
32    #include <gtkmm/box.h>
33  #include <gtkmm/adjustment.h>  #include <gtkmm/adjustment.h>
34  #include <gtkmm/alignment.h>  #include <gtkmm/alignment.h>
35  #include <gtkmm/box.h>  #include <gtkmm/checkbutton.h>
36  #include <gtkmm/comboboxtext.h>  #include <gtkmm/comboboxtext.h>
37    #include <gtkmm/frame.h>
38  #include <gtkmm/label.h>  #include <gtkmm/label.h>
39  #include <gtkmm/scale.h>  #include <gtkmm/scale.h>
40  #include <gtkmm/spinbutton.h>  #include <gtkmm/spinbutton.h>
41    #include <gtkmm/table.h>
42    #include <gtkmm/textview.h>
43    
44    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 12) || GTKMM_MAJOR_VERSION < 2
45    #define OLD_TOOLTIPS
46  #include <gtkmm/tooltips.h>  #include <gtkmm/tooltips.h>
47    #endif
48    
49    int note_value(const Glib::ustring& note);
50    Glib::ustring note_str(int note);
51    
52    void spin_button_show_notes(Gtk::SpinButton& spin_button);
53    
54  class LabelWidget {  class LabelWidget {
55  public:  public:
# Line 38  public: Line 58  public:
58    
59      LabelWidget(const char* labelText, Gtk::Widget& widget);      LabelWidget(const char* labelText, Gtk::Widget& widget);
60      void set_sensitive(bool sensitive = true);      void set_sensitive(bool sensitive = true);
61      sigc::signal<void> signal_changed_by_user() {      sigc::signal<void>& signal_value_changed() {
62          return sig_changed;          return sig_changed;
63      }      }
64  protected:  protected:
65    #ifdef OLD_TOOLTIPS
66      Gtk::Tooltips tooltips;      Gtk::Tooltips tooltips;
67    #endif
68      sigc::signal<void> sig_changed;      sigc::signal<void> sig_changed;
69  };  };
70    
71    class ReadOnlyLabelWidget : public LabelWidget {
72    public:
73        Gtk::Label text;
74    
75        ReadOnlyLabelWidget(const char* leftHandText);
76        ReadOnlyLabelWidget(const char* leftHandText, const char* rightHandText);
77    };
78    
79  class NumEntry : public LabelWidget {  class NumEntry : public LabelWidget {
80  protected:  protected:
81    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
82      Gtk::Adjustment adjust;      Gtk::Adjustment adjust;
83    #else
84        Glib::RefPtr<Gtk::Adjustment> adjust;
85    #endif
86      Gtk::HScale scale;      Gtk::HScale scale;
87      Gtk::SpinButton spinbutton;      Gtk::SpinButton spinbutton;
88      Gtk::HBox box;      Gtk::HBox box;
89    
90      int round_to_int(double x) {      static int round_to_int(double x) {
91          return int(x < 0.0 ? x - 0.5 : x + 0.5);          return int(x < 0.0 ? x - 0.5 : x + 0.5);
92      }      }
93  public:  public:
94      NumEntry(const char* labelText, double lower = 0, double upper = 127,      NumEntry(const char* labelText, double lower = 0, double upper = 127,
95               int decimals = 0);               int decimals = 0);
     void set_value(double value) {  
         spinbutton.set_value(value);  
     }  
     double get_value() const {  
         return spinbutton.get_value();  
     }  
96      void set_tip(const Glib::ustring& tip_text) {      void set_tip(const Glib::ustring& tip_text) {
97    #ifdef OLD_TOOLTIPS
98          tooltips.set_tip(spinbutton, tip_text);          tooltips.set_tip(spinbutton, tip_text);
99    #else
100            spinbutton.set_tooltip_text(tip_text);
101    #endif
102      }      }
103      void set_upper(double upper) {      void set_upper(double upper) {
104    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
105          adjust.set_upper(upper);          adjust.set_upper(upper);
106    #else
107            adjust->set_upper(upper);
108    #endif
109      }      }
110  };  };
111    
112  class NumEntryGain : public NumEntry {  class NumEntryGain : public NumEntry {
113  private:  private:
114        int32_t value;
115      void value_changed();      void value_changed();
     int32_t* ptr;  
116      double coeff;      double coeff;
117        bool connected;
118  public:  public:
119      NumEntryGain(const char* labelText,      NumEntryGain(const char* labelText,
120                   double lower, double upper, int decimals, double coeff);                   double lower, double upper, int decimals, double coeff);
121      void set_ptr(int32_t* ptr);      int32_t get_value() const { return value; }
122        void set_value(int32_t value);
123  };  };
124    
125  template<typename T>  template<typename T>
126  class NumEntryTemp : public NumEntry {  class NumEntryTemp : public NumEntry {
127  private:  private:
128      T* ptr;      T value;
129      void value_changed();      void value_changed();
130  public:  public:
131      NumEntryTemp(const char* labelText,      NumEntryTemp(const char* labelText,
132                   double lower = 0, double upper = 127, int decimals = 0);                   double lower = 0, double upper = 127, int decimals = 0);
133      void set_ptr(T* ptr);      T get_value() const { return value; }
134        void set_value(T value);
135  };  };
136    
137  template<typename T>  template<typename T>
138  NumEntryTemp<T>::NumEntryTemp(const char* labelText,  NumEntryTemp<T>::NumEntryTemp(const char* labelText,
139                                double lower, double upper, int decimals) :                                double lower, double upper, int decimals) :
140      NumEntry(labelText, lower, upper, decimals)      NumEntry(labelText, lower, upper, decimals),
141        value(0)
142  {  {
143      spinbutton.signal_value_changed().connect(      spinbutton.signal_value_changed().connect(
144          sigc::mem_fun(*this, &NumEntryTemp::value_changed));          sigc::mem_fun(*this, &NumEntryTemp::value_changed));
# Line 107  NumEntryTemp<T>::NumEntryTemp(const char Line 147  NumEntryTemp<T>::NumEntryTemp(const char
147  template<typename T>  template<typename T>
148  void NumEntryTemp<T>::value_changed()  void NumEntryTemp<T>::value_changed()
149  {  {
150      if (ptr) {      const double f = pow(10, spinbutton.get_digits());
151          const double f = pow(10, spinbutton.get_digits());      int new_value = round_to_int(spinbutton.get_value() * f);
152          int new_value = round_to_int(spinbutton.get_value() * f);      if (new_value != round_to_int(value * f)) {
153          if (new_value != round_to_int(*ptr * f)) {          value = T(new_value / f);
154              *ptr = T(new_value / f);          sig_changed();
             sig_changed();  
         }  
155      }      }
156  }  }
157    
158  template<typename T>  template<typename T>
159  void NumEntryTemp<T>::set_ptr(T* ptr)  void NumEntryTemp<T>::set_value(T value)
160  {  {
161      this->ptr = 0;  #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
162      if (ptr) set_value(*ptr);      if (value > adjust.get_upper()) value = T(adjust.get_upper());
163      this->ptr = ptr;  #else
164        if (value > adjust->get_upper()) value = T(adjust->get_upper());
165    #endif
166        if (this->value != value) {
167            this->value = value;
168            const double f = pow(10, spinbutton.get_digits());
169            if (round_to_int(spinbutton.get_value() * f) != round_to_int(value * f)) {
170                spinbutton.set_value(value);
171            }
172            sig_changed();
173        }
174  }  }
175    
176    
# Line 137  private: Line 185  private:
185    
186  class NumEntryPermille : public NumEntry {  class NumEntryPermille : public NumEntry {
187  private:  private:
188      uint16_t* ptr;      uint16_t value;
189      void value_changed();      void value_changed();
190  public:  public:
191      NumEntryPermille(const char* labelText,      NumEntryPermille(const char* labelText,
192                       double lower = 0, double upper = 127, int decimals = 0);                       double lower = 0, double upper = 127, int decimals = 0);
193      void set_ptr(uint16_t* ptr);      uint16_t get_value() const { return value; }
194        void set_value(uint16_t value);
195  };  };
196    
197    
# Line 151  class ChoiceEntry : public LabelWidget { Line 200  class ChoiceEntry : public LabelWidget {
200  private:  private:
201      Gtk::ComboBoxText combobox;      Gtk::ComboBoxText combobox;
202      Gtk::Alignment align;      Gtk::Alignment align;
     T* ptr;  
     void value_changed();  
203      const T* values;      const T* values;
204  public:  public:
205      ChoiceEntry(const char* labelText);      ChoiceEntry(const char* labelText);
206        T get_value() const;
207        void set_value(T value);
208      void set_choices(const char** texts, const T* values);      void set_choices(const char** texts, const T* values);
209      void set_ptr(T* ptr);  
     int get_active_row_number() { return combobox.get_active_row_number(); }  
     Glib::SignalProxy0<void> signal_changed() {  
         return combobox.signal_changed();  
     }  
210      void set_tip(const Glib::ustring& tip_text) {      void set_tip(const Glib::ustring& tip_text) {
211          tooltips.set_tip(combobox, tip_text); //FIXME: don't Gtk::ComboBoxes support tooltips ???  #ifdef OLD_TOOLTIPS
212            tooltips.set_tip(combobox, tip_text);
213    #else
214            combobox.set_tooltip_text(tip_text);
215    #endif
216      }      }
217  };  };
218    
219  template<typename T>  template<typename T>
220  ChoiceEntry<T>::ChoiceEntry(const char* labelText) :  ChoiceEntry<T>::ChoiceEntry(const char* labelText) :
221        LabelWidget(labelText, align),
222      align(0, 0, 0, 0),      align(0, 0, 0, 0),
223      LabelWidget(labelText, align)      values(0)
224  {  {
225      combobox.signal_changed().connect(      combobox.signal_changed().connect(sig_changed.make_slot());
         sigc::mem_fun(*this, &ChoiceEntry::value_changed));  
226      align.add(combobox);      align.add(combobox);
227  }  }
228    
# Line 181  template<typename T> Line 230  template<typename T>
230  void ChoiceEntry<T>::set_choices(const char** texts, const T* values)  void ChoiceEntry<T>::set_choices(const char** texts, const T* values)
231  {  {
232      for (int i = 0 ; texts[i] ; i++) {      for (int i = 0 ; texts[i] ; i++) {
233    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 24) || GTKMM_MAJOR_VERSION < 2
234          combobox.append_text(texts[i]);          combobox.append_text(texts[i]);
235    #else
236            combobox.append(texts[i]);
237    #endif
238      }      }
239      this->values = values;      this->values = values;
240  }  }
241    
242  template<typename T>  template<typename T>
243  void ChoiceEntry<T>::value_changed()  T ChoiceEntry<T>::get_value() const
244  {  {
245      if (ptr) {      int rowno = combobox.get_active_row_number();
246          int rowno = combobox.get_active_row_number();      return values[rowno];
         if (rowno != -1) {  
             *ptr = values[rowno];  
             sig_changed();  
         }  
     }  
247  }  }
248    
249  template<typename T>  template<typename T>
250  void ChoiceEntry<T>::set_ptr(T* ptr)  void ChoiceEntry<T>::set_value(T value)
251  {  {
252      this->ptr = 0;      int row = 0;
253      if (ptr) {      int nb_rows = combobox.get_model()->children().size();
254          T value = *ptr;      for (; row < nb_rows ; row++) {
255          int row = 0;          if (value == values[row]) break;
256          int nb_rows = combobox.get_model()->children().size();      }
257          for (; row < nb_rows ; row++) {      combobox.set_active(row == nb_rows ? -1 : row);
             if (value == values[row]) break;  
         }  
         combobox.set_active(row == nb_rows ? -1 : row);  
     } else combobox.set_active(-1);  
     this->ptr = ptr;  
258  }  }
259    
260    
261  class ChoiceEntryLeverageCtrl : public LabelWidget {  class ChoiceEntryLeverageCtrl : public LabelWidget {
262  private:  private:
263        gig::leverage_ctrl_t value;
264      Gtk::ComboBoxText combobox;      Gtk::ComboBoxText combobox;
265      Gtk::Alignment align;      Gtk::Alignment align;
     gig::leverage_ctrl_t* ptr;  
266      void value_changed();      void value_changed();
267  public:  public:
268      ChoiceEntryLeverageCtrl(const char* labelText);      ChoiceEntryLeverageCtrl(const char* labelText);
269      void set_ptr(gig::leverage_ctrl_t* ptr);      gig::leverage_ctrl_t get_value() const { return value; }
270      int get_active_row_number() { return combobox.get_active_row_number(); }      void set_value(gig::leverage_ctrl_t value);
271      Glib::SignalProxy0<void> signal_changed() {      void set_tip(const Glib::ustring& tip_text) {
272          return combobox.signal_changed();          combobox.set_tooltip_text(tip_text);
273      }      }
274  };  };
275    
# Line 234  public: Line 277  public:
277  class BoolEntry : public LabelWidget {  class BoolEntry : public LabelWidget {
278  private:  private:
279      Gtk::CheckButton checkbutton;      Gtk::CheckButton checkbutton;
     bool* ptr;  
     void value_changed();  
280  public:  public:
281      BoolEntry(const char* labelText);      BoolEntry(const char* labelText);
282      bool get_active() { return checkbutton.get_active(); }      bool get_value() const { return checkbutton.get_active(); }
283      bool set_active(bool b) { checkbutton.set_active(b); }      void set_value(bool value) { checkbutton.set_active(value); }
284      Glib::SignalProxy0<void> signal_toggled() {  
         return checkbutton.signal_toggled();  
     }  
     void set_ptr(bool* ptr);  
285      void set_tip(const Glib::ustring& tip_text) {      void set_tip(const Glib::ustring& tip_text) {
286    #ifdef OLD_TOOLTIPS
287          tooltips.set_tip(checkbutton, tip_text);          tooltips.set_tip(checkbutton, tip_text);
288    #else
289            checkbutton.set_tooltip_text(tip_text);
290    #endif
291        }
292    };
293    
294    class BoolBox : public Gtk::CheckButton {
295    public:
296        BoolBox(const char* labelText) : Gtk::CheckButton(labelText) {
297            signal_toggled().connect(sig_changed.make_slot());
298      }      }
299        bool get_value() const { return get_active(); }
300        void set_value(bool value) { set_active(value); }
301        sigc::signal<void>& signal_value_changed() { return sig_changed; }
302    protected:
303        sigc::signal<void> sig_changed;
304  };  };
305    
306    
307  class BoolEntryPlus6 : public LabelWidget {  class BoolEntryPlus6 : public LabelWidget {
308  private:  private:
309      Gtk::CheckButton checkbutton;      Gtk::CheckButton checkbutton;
     int32_t* ptr;  
310      void value_changed();      void value_changed();
311      NumEntryGain& eGain;      NumEntryGain& eGain;
312      int32_t plus6value;      int32_t plus6value;
313  public:  public:
314      BoolEntryPlus6(const char* labelText, NumEntryGain& eGain, int32_t plus6value);      BoolEntryPlus6(const char* labelText, NumEntryGain& eGain, int32_t plus6value);
315      void set_ptr(int32_t* ptr);      int32_t get_value() const;
316      bool get_active() { return checkbutton.get_active(); }      void set_value(int32_t value);
     Glib::SignalProxy0<void> signal_toggled() {  
         return checkbutton.signal_toggled();  
     }  
317  };  };
318    
319    
320  class StringEntry : public LabelWidget {  class StringEntry : public LabelWidget {
321  private:  private:
322      Gtk::Entry entry;      Gtk::Entry entry;
     gig::String* ptr;  
     void value_changed();  
323  public:  public:
324      StringEntry(const char* labelText);      StringEntry(const char* labelText);
325      void set_ptr(gig::String* ptr);      gig::String get_value() const;
326        void set_value(const gig::String& value);
327        void set_width_chars(int n_chars) { entry.set_width_chars(n_chars); }
328    };
329    
330    class StringEntryMultiLine : public LabelWidget {
331    private:
332        Gtk::TextView text_view;
333        Glib::RefPtr<Gtk::TextBuffer> text_buffer;
334        Gtk::Frame frame;
335    public:
336        StringEntryMultiLine(const char* labelText);
337        gig::String get_value() const;
338        void set_value(const gig::String& value);
339    };
340    
341    
342    /**
343     * Container widget for LabelWidgets.
344     */
345    class Table : public Gtk::Table
346    {
347    public:
348        Table(int x, int y);
349        void add(BoolEntry& boolentry);
350        void add(BoolEntryPlus6& boolentry);
351        void add(LabelWidget& labelwidget);
352    private:
353        int rowno;
354  };  };
355    
356    
357    /**
358     * Base class for editor components that use LabelWidgets to edit
359     * member variables of the same class. By connecting the widgets to
360     * members of the model class, the model is automatically kept
361     * updated.
362     */
363    template<class M>
364    class PropEditor {
365    public:
366        sigc::signal<void>& signal_changed() {
367            return sig_changed;
368        }
369    protected:
370        M* m;
371        int update_model; // to prevent infinite update loops
372        PropEditor() : m(0), update_model(0) { }
373        sigc::signal<void> sig_changed;
374    
375        template<class C, typename T>
376        void connect(C& widget, T M::* member) {
377            // gcc 4.1.2 needs this temporary variable to resolve the
378            // address
379            void (PropEditor::*f)(const C* w, T M::* member) =
380                &PropEditor::set_member;
381            widget.signal_value_changed().connect(
382                sigc::bind(sigc::mem_fun(*this, f), &widget, member));
383    
384            void (PropEditor::*g)(C* w, T M::* member) =
385                &PropEditor::get_member;
386            sig.connect(
387                sigc::bind(sigc::mem_fun(*this, g), &widget, member));
388        }
389    
390        template<class C, class S, typename T>
391        void connect(C& widget, void (S::*setter)(T)) {
392            void (PropEditor::*f)(const C* w, void (S::*setter)(T)) =
393                &PropEditor<M>::call_setter;
394            widget.signal_value_changed().connect(
395                sigc::bind(sigc::mem_fun(*this, f), &widget, setter));
396        }
397    
398        void connect(NoteEntry& eKeyRangeLow, NoteEntry& eKeyRangeHigh,
399                     gig::range_t M::* range) {
400            eKeyRangeLow.signal_value_changed().connect(
401                sigc::bind(
402                    sigc::mem_fun(*this, &PropEditor::key_range_low_changed),
403                    &eKeyRangeLow, &eKeyRangeHigh, range));
404            eKeyRangeHigh.signal_value_changed().connect(
405                sigc::bind(
406                    sigc::mem_fun(*this, &PropEditor::key_range_high_changed),
407                    &eKeyRangeLow, &eKeyRangeHigh, range));
408            sig.connect(
409                sigc::bind(sigc::mem_fun(*this, &PropEditor::get_key_range),
410                           &eKeyRangeLow, &eKeyRangeHigh, range));
411        }
412    
413        void update(M* m) {
414            update_model++;
415            this->m = m;
416            sig.emit();
417            update_model--;
418        }
419    
420    private:
421        sigc::signal<void> sig;
422    
423        void key_range_low_changed(NoteEntry* eKeyRangeLow,
424                                   NoteEntry* eKeyRangeHigh,
425                                   gig::range_t M::* range) {
426            if (update_model == 0) {
427                uint8_t value = eKeyRangeLow->get_value();
428                (m->*range).low = value;
429                if (value > (m->*range).high) {
430                    eKeyRangeHigh->set_value(value);
431                }
432                sig_changed();
433            }
434        }
435    
436        void key_range_high_changed(NoteEntry* eKeyRangeLow,
437                                    NoteEntry* eKeyRangeHigh,
438                                    gig::range_t M::* range) {
439            if (update_model == 0) {
440                uint8_t value = eKeyRangeHigh->get_value();
441                (m->*range).high = value;
442                if (value < (m->*range).low) {
443                    eKeyRangeLow->set_value(value);
444                }
445                sig_changed();
446            }
447        }
448    
449        template<class C, typename T>
450        void set_member(const C* w, T M::* member) {
451            if (update_model == 0) {
452                m->*member = w->get_value();
453                sig_changed();
454            }
455        }
456    
457        template<class C, typename T>
458        void get_member(C* w, T M::* member) {
459            w->set_value(m->*member);
460        }
461    
462        void get_key_range(NoteEntry* eKeyRangeLow,
463                           NoteEntry* eKeyRangeHigh,
464                           gig::range_t M::* range) const {
465            eKeyRangeLow->set_value((m->*range).low);
466            eKeyRangeHigh->set_value((m->*range).high);
467        }
468    
469        template<class C, class S, typename T>
470        void call_setter(const C* w, void (S::*setter)(T)) {
471            if (update_model == 0) {
472                (static_cast<S*>(this)->*setter)(w->get_value());
473                sig_changed();
474            }
475        }
476    };
477    
478  #endif  #endif

Legend:
Removed from v.1261  
changed lines
  Added in v.3329

  ViewVC Help
Powered by ViewVC