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

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

  ViewVC Help
Powered by ViewVC