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

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

  ViewVC Help
Powered by ViewVC