/[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 3068 - (hide annotations) (download) (as text)
Mon Jan 2 22:13:01 2017 UTC (7 years, 3 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 13076 byte(s)
- Preparations for Xcode project update.

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

  ViewVC Help
Powered by ViewVC