/[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 1460 by persson, Sat Oct 27 12:28:33 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 <math.h>  #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/table.h>
37    #include <gtkmm/textview.h>
38    
39    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 12) || GTKMM_MAJOR_VERSION < 2
40    #define OLD_TOOLTIPS
41  #include <gtkmm/tooltips.h>  #include <gtkmm/tooltips.h>
42    #endif
43    
44  class LabelWidget {  class LabelWidget {
45  public:  public:
# Line 44  public: Line 52  public:
52          return sig_changed;          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;      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;
# Line 62  public: Line 76  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);
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    
# Line 118  void NumEntryTemp<T>::value_changed() Line 140  void NumEntryTemp<T>::value_changed()
140  template<typename T>  template<typename T>
141  void NumEntryTemp<T>::set_value(T value)  void NumEntryTemp<T>::set_value(T value)
142  {  {
143    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
144      if (value > adjust.get_upper()) value = T(adjust.get_upper());      if (value > adjust.get_upper()) value = T(adjust.get_upper());
145    #else
146        if (value > adjust->get_upper()) value = T(adjust->get_upper());
147    #endif
148      if (this->value != value) {      if (this->value != value) {
149          this->value = value;          this->value = value;
150          const double f = pow(10, spinbutton.get_digits());          const double f = pow(10, spinbutton.get_digits());
# Line 164  public: Line 190  public:
190      void set_choices(const char** texts, const T* values);      void set_choices(const char** texts, const T* values);
191    
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(sig_changed.make_slot());      combobox.signal_changed().connect(sig_changed.make_slot());
207      align.add(combobox);      align.add(combobox);
# Line 181  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  }  }
# Line 227  public: Line 261  public:
261      void set_value(bool value) { checkbutton.set_active(value); }      void set_value(bool value) { checkbutton.set_active(value); }
262    
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 247  public: Line 285  public:
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.1460  
changed lines
  Added in v.2423

  ViewVC Help
Powered by ViewVC