/[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 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 <math.h>  #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 44  public: Line 58  public:
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 62  public: Line 82  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);
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    
# Line 118  void NumEntryTemp<T>::value_changed() Line 146  void NumEntryTemp<T>::value_changed()
146  template<typename T>  template<typename T>
147  void NumEntryTemp<T>::set_value(T value)  void NumEntryTemp<T>::set_value(T value)
148  {  {
149    #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
150      if (value > adjust.get_upper()) value = T(adjust.get_upper());      if (value > adjust.get_upper()) value = T(adjust.get_upper());
151    #else
152        if (value > adjust->get_upper()) value = T(adjust->get_upper());
153    #endif
154      if (this->value != value) {      if (this->value != value) {
155          this->value = value;          this->value = value;
156          const double f = pow(10, spinbutton.get_digits());          const double f = pow(10, spinbutton.get_digits());
# Line 164  public: Line 196  public:
196      void set_choices(const char** texts, const T* values);      void set_choices(const char** texts, const T* values);
197    
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(sig_changed.make_slot());      combobox.signal_changed().connect(sig_changed.make_slot());
213      align.add(combobox);      align.add(combobox);
# 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  }  }
# Line 227  public: Line 267  public:
267      void set_value(bool value) { checkbutton.set_active(value); }      void set_value(bool value) { checkbutton.set_active(value); }
268    
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 244  public: Line 288  public:
288      void set_value(int32_t value);      void set_value(int32_t value);
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.1460  
changed lines
  Added in v.2446

  ViewVC Help
Powered by ViewVC