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

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

  ViewVC Help
Powered by ViewVC