/[svn]/gigedit/trunk/src/gigedit/paramedit.h
ViewVC logotype

Annotation of /gigedit/trunk/src/gigedit/paramedit.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2566 - (hide annotations) (download) (as text)
Tue May 20 14:35:36 2014 UTC (9 years, 10 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 12775 byte(s)
* Minor usability improvements (UI labels and tooltips).

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

  ViewVC Help
Powered by ViewVC