/[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 3462 - (hide annotations) (download) (as text)
Sun Feb 3 13:27:28 2019 UTC (5 years, 2 months ago) by persson
File MIME type: text/x-c++hdr
File size: 14209 byte(s)
* Fix build on old linux environments
* Enable "silent rules" in build

1 schoenebeck 1225 /* -*- c++ -*-
2 persson 3462 * Copyright (C) 2006-2019 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 schoenebeck 3068 #ifdef LIBGIG_HEADER_FILE
24     # include LIBGIG_HEADER_FILE(gig.h)
25     #else
26     # include <gig.h>
27     #endif
28 schoenebeck 1225
29 persson 2151 #include <cmath>
30 schoenebeck 1300
31 schoenebeck 3364 #include "compat.h"
32    
33 persson 2446 #include <glibmm/convert.h>
34 persson 2844 #include <gtkmm/box.h>
35 schoenebeck 1225 #include <gtkmm/adjustment.h>
36 schoenebeck 3364 #if HAS_GTKMM_ALIGNMENT
37     # include <gtkmm/alignment.h>
38     #endif
39 persson 2169 #include <gtkmm/checkbutton.h>
40 schoenebeck 1225 #include <gtkmm/comboboxtext.h>
41 persson 1582 #include <gtkmm/frame.h>
42 schoenebeck 1225 #include <gtkmm/label.h>
43     #include <gtkmm/scale.h>
44     #include <gtkmm/spinbutton.h>
45 schoenebeck 3364 #if USE_GTKMM_GRID
46     # include <gtkmm/grid.h>
47     #else
48     # include <gtkmm/table.h>
49     #endif
50 persson 1582 #include <gtkmm/textview.h>
51 persson 2151
52     #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 12) || GTKMM_MAJOR_VERSION < 2
53     #define OLD_TOOLTIPS
54 schoenebeck 1225 #include <gtkmm/tooltips.h>
55 persson 2151 #endif
56 schoenebeck 1225
57 persson 2507 int note_value(const Glib::ustring& note);
58     Glib::ustring note_str(int note);
59 persson 2446
60 persson 2507 void spin_button_show_notes(Gtk::SpinButton& spin_button);
61    
62 schoenebeck 1225 class LabelWidget {
63     public:
64     Gtk::Label label;
65     Gtk::Widget& widget;
66    
67     LabelWidget(const char* labelText, Gtk::Widget& widget);
68     void set_sensitive(bool sensitive = true);
69 persson 1460 sigc::signal<void>& signal_value_changed() {
70 persson 1261 return sig_changed;
71     }
72 schoenebeck 1225 protected:
73 schoenebeck 3409 virtual void on_show_tooltips_changed();
74 persson 2151 #ifdef OLD_TOOLTIPS
75 schoenebeck 1225 Gtk::Tooltips tooltips;
76 persson 2151 #endif
77 persson 1261 sigc::signal<void> sig_changed;
78 schoenebeck 1225 };
79    
80 schoenebeck 2690 class ReadOnlyLabelWidget : public LabelWidget {
81     public:
82     Gtk::Label text;
83    
84     ReadOnlyLabelWidget(const char* leftHandText);
85     ReadOnlyLabelWidget(const char* leftHandText, const char* rightHandText);
86     };
87    
88 schoenebeck 1225 class NumEntry : public LabelWidget {
89     protected:
90 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
91 schoenebeck 1225 Gtk::Adjustment adjust;
92 persson 2169 #else
93     Glib::RefPtr<Gtk::Adjustment> adjust;
94     #endif
95 schoenebeck 3364 HScale scale;
96 schoenebeck 1225 Gtk::SpinButton spinbutton;
97 schoenebeck 3364 HBox box;
98 persson 1261
99 persson 2841 static int round_to_int(double x) {
100 persson 1261 return int(x < 0.0 ? x - 0.5 : x + 0.5);
101     }
102 schoenebeck 3409
103 schoenebeck 1225 public:
104     NumEntry(const char* labelText, double lower = 0, double upper = 127,
105     int decimals = 0);
106     void set_tip(const Glib::ustring& tip_text) {
107 persson 2151 #ifdef OLD_TOOLTIPS
108 schoenebeck 1225 tooltips.set_tip(spinbutton, tip_text);
109 persson 2151 #else
110     spinbutton.set_tooltip_text(tip_text);
111     #endif
112 schoenebeck 1225 }
113     void set_upper(double upper) {
114 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
115 schoenebeck 1225 adjust.set_upper(upper);
116 persson 2169 #else
117     adjust->set_upper(upper);
118     #endif
119 schoenebeck 1225 }
120 schoenebeck 3409 void on_show_tooltips_changed();
121 schoenebeck 1225 };
122    
123     class NumEntryGain : public NumEntry {
124     private:
125 persson 1460 int32_t value;
126 schoenebeck 1225 void value_changed();
127     double coeff;
128 persson 1460 bool connected;
129 schoenebeck 1225 public:
130     NumEntryGain(const char* labelText,
131     double lower, double upper, int decimals, double coeff);
132 persson 1460 int32_t get_value() const { return value; }
133     void set_value(int32_t value);
134 schoenebeck 1225 };
135    
136     template<typename T>
137     class NumEntryTemp : public NumEntry {
138     private:
139 persson 1460 T value;
140 schoenebeck 1225 void value_changed();
141     public:
142     NumEntryTemp(const char* labelText,
143     double lower = 0, double upper = 127, int decimals = 0);
144 persson 1460 T get_value() const { return value; }
145     void set_value(T value);
146 schoenebeck 1225 };
147    
148     template<typename T>
149     NumEntryTemp<T>::NumEntryTemp(const char* labelText,
150     double lower, double upper, int decimals) :
151 persson 1460 NumEntry(labelText, lower, upper, decimals),
152     value(0)
153 schoenebeck 1225 {
154     spinbutton.signal_value_changed().connect(
155     sigc::mem_fun(*this, &NumEntryTemp::value_changed));
156     }
157    
158     template<typename T>
159     void NumEntryTemp<T>::value_changed()
160     {
161 schoenebeck 1359 const double f = pow(10, spinbutton.get_digits());
162     int new_value = round_to_int(spinbutton.get_value() * f);
163 persson 1460 if (new_value != round_to_int(value * f)) {
164     value = T(new_value / f);
165     sig_changed();
166 schoenebeck 1225 }
167     }
168    
169     template<typename T>
170 persson 1460 void NumEntryTemp<T>::set_value(T value)
171 schoenebeck 1225 {
172 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
173 persson 1460 if (value > adjust.get_upper()) value = T(adjust.get_upper());
174 persson 2169 #else
175     if (value > adjust->get_upper()) value = T(adjust->get_upper());
176     #endif
177 persson 1460 if (this->value != value) {
178     this->value = value;
179     const double f = pow(10, spinbutton.get_digits());
180     if (round_to_int(spinbutton.get_value() * f) != round_to_int(value * f)) {
181     spinbutton.set_value(value);
182     }
183     sig_changed();
184     }
185 schoenebeck 1225 }
186    
187    
188     class NoteEntry : public NumEntryTemp<uint8_t> {
189     public:
190     NoteEntry(const char* labelText);
191     private:
192     int on_input(double* new_value);
193     bool on_output();
194     };
195    
196    
197     class NumEntryPermille : public NumEntry {
198     private:
199 persson 1460 uint16_t value;
200 schoenebeck 1225 void value_changed();
201     public:
202     NumEntryPermille(const char* labelText,
203     double lower = 0, double upper = 127, int decimals = 0);
204 persson 1460 uint16_t get_value() const { return value; }
205     void set_value(uint16_t value);
206 schoenebeck 1225 };
207    
208 schoenebeck 3409 class ChoiceEntryBase : public LabelWidget {
209     protected:
210     ChoiceEntryBase(const char* labelText, Gtk::Widget& widget) : LabelWidget(labelText, widget) {};
211     Gtk::ComboBoxText combobox;
212     void on_show_tooltips_changed();
213     };
214 schoenebeck 1225
215     template<typename T>
216 schoenebeck 3409 class ChoiceEntry : public ChoiceEntryBase {
217 schoenebeck 1225 private:
218 schoenebeck 3364 #if HAS_GTKMM_ALIGNMENT
219 schoenebeck 1225 Gtk::Alignment align;
220 schoenebeck 3364 #endif
221 schoenebeck 1225 const T* values;
222     public:
223     ChoiceEntry(const char* labelText);
224 persson 1460 T get_value() const;
225     void set_value(T value);
226 schoenebeck 1225 void set_choices(const char** texts, const T* values);
227 persson 1460
228 schoenebeck 1225 void set_tip(const Glib::ustring& tip_text) {
229 persson 2151 #ifdef OLD_TOOLTIPS
230     tooltips.set_tip(combobox, tip_text);
231     #else
232     combobox.set_tooltip_text(tip_text);
233     #endif
234 schoenebeck 1225 }
235     };
236    
237     template<typename T>
238     ChoiceEntry<T>::ChoiceEntry(const char* labelText) :
239 schoenebeck 3364 #if HAS_GTKMM_ALIGNMENT
240 schoenebeck 3409 ChoiceEntryBase(labelText, align),
241 persson 2507 align(0, 0, 0, 0),
242 schoenebeck 3364 #else
243 schoenebeck 3409 ChoiceEntryBase(labelText, combobox),
244 schoenebeck 3364 #endif
245 persson 2507 values(0)
246 schoenebeck 1225 {
247 persson 1460 combobox.signal_changed().connect(sig_changed.make_slot());
248 schoenebeck 3364 #if HAS_GTKMM_ALIGNMENT
249 schoenebeck 1225 align.add(combobox);
250 schoenebeck 3364 #endif
251 schoenebeck 1225 }
252    
253     template<typename T>
254     void ChoiceEntry<T>::set_choices(const char** texts, const T* values)
255     {
256 persson 3462 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 24) || GTKMM_MAJOR_VERSION < 2
257     combobox.clear_items();
258 schoenebeck 1225 for (int i = 0 ; texts[i] ; i++) {
259     combobox.append_text(texts[i]);
260 persson 3462 }
261 persson 2169 #else
262 persson 3462 combobox.remove_all();
263     for (int i = 0 ; texts[i] ; i++) {
264 persson 2169 combobox.append(texts[i]);
265 persson 3462 }
266 persson 2169 #endif
267 schoenebeck 1225 this->values = values;
268     }
269    
270     template<typename T>
271 persson 1460 T ChoiceEntry<T>::get_value() const
272 schoenebeck 1225 {
273 schoenebeck 1359 int rowno = combobox.get_active_row_number();
274 persson 1460 return values[rowno];
275 schoenebeck 1225 }
276    
277     template<typename T>
278 persson 1460 void ChoiceEntry<T>::set_value(T value)
279 schoenebeck 1225 {
280 persson 1460 int row = 0;
281     int nb_rows = combobox.get_model()->children().size();
282     for (; row < nb_rows ; row++) {
283     if (value == values[row]) break;
284     }
285     combobox.set_active(row == nb_rows ? -1 : row);
286 schoenebeck 1225 }
287    
288    
289     class ChoiceEntryLeverageCtrl : public LabelWidget {
290     private:
291 persson 1460 gig::leverage_ctrl_t value;
292 schoenebeck 1225 Gtk::ComboBoxText combobox;
293 schoenebeck 3364 #if HAS_GTKMM_ALIGNMENT
294 schoenebeck 1225 Gtk::Alignment align;
295 schoenebeck 3364 #endif
296 schoenebeck 1225 void value_changed();
297 schoenebeck 3409 protected:
298     void on_show_tooltips_changed();
299 schoenebeck 1225 public:
300     ChoiceEntryLeverageCtrl(const char* labelText);
301 persson 1460 gig::leverage_ctrl_t get_value() const { return value; }
302     void set_value(gig::leverage_ctrl_t value);
303 schoenebeck 2566 void set_tip(const Glib::ustring& tip_text) {
304     combobox.set_tooltip_text(tip_text);
305     }
306 schoenebeck 1225 };
307    
308    
309     class BoolEntry : public LabelWidget {
310     private:
311     Gtk::CheckButton checkbutton;
312     public:
313     BoolEntry(const char* labelText);
314 persson 1460 bool get_value() const { return checkbutton.get_active(); }
315     void set_value(bool value) { checkbutton.set_active(value); }
316    
317 schoenebeck 1225 void set_tip(const Glib::ustring& tip_text) {
318 persson 2151 #ifdef OLD_TOOLTIPS
319 schoenebeck 1225 tooltips.set_tip(checkbutton, tip_text);
320 persson 2151 #else
321     checkbutton.set_tooltip_text(tip_text);
322     #endif
323 schoenebeck 1225 }
324     };
325    
326 schoenebeck 3329 class BoolBox : public Gtk::CheckButton {
327     public:
328 schoenebeck 3409 BoolBox(const char* labelText);
329 schoenebeck 3329 bool get_value() const { return get_active(); }
330     void set_value(bool value) { set_active(value); }
331     sigc::signal<void>& signal_value_changed() { return sig_changed; }
332     protected:
333 schoenebeck 3409 void on_show_tooltips_changed();
334    
335 schoenebeck 3329 sigc::signal<void> sig_changed;
336     };
337 schoenebeck 1225
338 schoenebeck 3329
339 schoenebeck 1225 class BoolEntryPlus6 : public LabelWidget {
340     private:
341     Gtk::CheckButton checkbutton;
342     void value_changed();
343     NumEntryGain& eGain;
344     int32_t plus6value;
345 schoenebeck 3409 protected:
346     void on_show_tooltips_changed();
347 schoenebeck 1225 public:
348     BoolEntryPlus6(const char* labelText, NumEntryGain& eGain, int32_t plus6value);
349 persson 1460 int32_t get_value() const;
350     void set_value(int32_t value);
351 schoenebeck 1225 };
352    
353 persson 2446
354 schoenebeck 1225 class StringEntry : public LabelWidget {
355     private:
356     Gtk::Entry entry;
357     public:
358     StringEntry(const char* labelText);
359 persson 2446 gig::String get_value() const;
360     void set_value(const gig::String& value);
361 persson 1582 void set_width_chars(int n_chars) { entry.set_width_chars(n_chars); }
362 schoenebeck 1225 };
363    
364 persson 1582 class StringEntryMultiLine : public LabelWidget {
365     private:
366     Gtk::TextView text_view;
367     Glib::RefPtr<Gtk::TextBuffer> text_buffer;
368     Gtk::Frame frame;
369 schoenebeck 3409 protected:
370     void on_show_tooltips_changed();
371 persson 1582 public:
372     StringEntryMultiLine(const char* labelText);
373     gig::String get_value() const;
374 persson 2446 void set_value(const gig::String& value);
375 persson 1582 };
376 schoenebeck 1225
377 persson 1582
378 persson 2423 /**
379     * Container widget for LabelWidgets.
380     */
381 schoenebeck 3364 class Table :
382     #if USE_GTKMM_GRID
383     public Gtk::Grid
384     #else
385     public Gtk::Table
386     #endif
387 persson 2423 {
388     public:
389     Table(int x, int y);
390     void add(BoolEntry& boolentry);
391     void add(BoolEntryPlus6& boolentry);
392     void add(LabelWidget& labelwidget);
393     private:
394 schoenebeck 3364 #if USE_GTKMM_GRID
395     int cols;
396     #endif
397 persson 2423 int rowno;
398     };
399    
400    
401     /**
402     * Base class for editor components that use LabelWidgets to edit
403     * member variables of the same class. By connecting the widgets to
404     * members of the model class, the model is automatically kept
405     * updated.
406     */
407     template<class M>
408     class PropEditor {
409     public:
410     sigc::signal<void>& signal_changed() {
411     return sig_changed;
412     }
413     protected:
414     M* m;
415     int update_model; // to prevent infinite update loops
416 persson 2507 PropEditor() : m(0), update_model(0) { }
417 persson 2423 sigc::signal<void> sig_changed;
418    
419     template<class C, typename T>
420     void connect(C& widget, T M::* member) {
421     // gcc 4.1.2 needs this temporary variable to resolve the
422     // address
423     void (PropEditor::*f)(const C* w, T M::* member) =
424     &PropEditor::set_member;
425     widget.signal_value_changed().connect(
426     sigc::bind(sigc::mem_fun(*this, f), &widget, member));
427    
428     void (PropEditor::*g)(C* w, T M::* member) =
429     &PropEditor::get_member;
430     sig.connect(
431     sigc::bind(sigc::mem_fun(*this, g), &widget, member));
432     }
433    
434     template<class C, class S, typename T>
435     void connect(C& widget, void (S::*setter)(T)) {
436     void (PropEditor::*f)(const C* w, void (S::*setter)(T)) =
437     &PropEditor<M>::call_setter;
438     widget.signal_value_changed().connect(
439     sigc::bind(sigc::mem_fun(*this, f), &widget, setter));
440     }
441    
442     void connect(NoteEntry& eKeyRangeLow, NoteEntry& eKeyRangeHigh,
443     gig::range_t M::* range) {
444     eKeyRangeLow.signal_value_changed().connect(
445     sigc::bind(
446     sigc::mem_fun(*this, &PropEditor::key_range_low_changed),
447     &eKeyRangeLow, &eKeyRangeHigh, range));
448     eKeyRangeHigh.signal_value_changed().connect(
449     sigc::bind(
450     sigc::mem_fun(*this, &PropEditor::key_range_high_changed),
451     &eKeyRangeLow, &eKeyRangeHigh, range));
452     sig.connect(
453     sigc::bind(sigc::mem_fun(*this, &PropEditor::get_key_range),
454     &eKeyRangeLow, &eKeyRangeHigh, range));
455     }
456    
457     void update(M* m) {
458     update_model++;
459     this->m = m;
460     sig.emit();
461     update_model--;
462     }
463    
464     private:
465     sigc::signal<void> sig;
466    
467     void key_range_low_changed(NoteEntry* eKeyRangeLow,
468     NoteEntry* eKeyRangeHigh,
469     gig::range_t M::* range) {
470     if (update_model == 0) {
471     uint8_t value = eKeyRangeLow->get_value();
472     (m->*range).low = value;
473     if (value > (m->*range).high) {
474     eKeyRangeHigh->set_value(value);
475     }
476     sig_changed();
477     }
478     }
479    
480     void key_range_high_changed(NoteEntry* eKeyRangeLow,
481     NoteEntry* eKeyRangeHigh,
482     gig::range_t M::* range) {
483     if (update_model == 0) {
484     uint8_t value = eKeyRangeHigh->get_value();
485     (m->*range).high = value;
486     if (value < (m->*range).low) {
487     eKeyRangeLow->set_value(value);
488     }
489     sig_changed();
490     }
491     }
492    
493     template<class C, typename T>
494     void set_member(const C* w, T M::* member) {
495     if (update_model == 0) {
496     m->*member = w->get_value();
497     sig_changed();
498     }
499     }
500    
501     template<class C, typename T>
502     void get_member(C* w, T M::* member) {
503     w->set_value(m->*member);
504     }
505    
506     void get_key_range(NoteEntry* eKeyRangeLow,
507     NoteEntry* eKeyRangeHigh,
508 persson 2841 gig::range_t M::* range) const {
509 persson 2423 eKeyRangeLow->set_value((m->*range).low);
510     eKeyRangeHigh->set_value((m->*range).high);
511     }
512    
513     template<class C, class S, typename T>
514     void call_setter(const C* w, void (S::*setter)(T)) {
515     if (update_model == 0) {
516     (static_cast<S*>(this)->*setter)(w->get_value());
517     sig_changed();
518     }
519     }
520     };
521    
522 schoenebeck 1225 #endif

  ViewVC Help
Powered by ViewVC