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

Legend:
Removed from v.1225  
changed lines
  Added in v.2423

  ViewVC Help
Powered by ViewVC