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

Legend:
Removed from v.1339  
changed lines
  Added in v.3364

  ViewVC Help
Powered by ViewVC