/[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 2446 - (hide annotations) (download) (as text)
Sun Apr 28 15:40:43 2013 UTC (10 years, 11 months ago) by persson
File MIME type: text/x-c++hdr
File size: 12516 byte(s)
* use character encoding Windows-1252 for all strings in gig files

1 schoenebeck 1225 /* -*- c++ -*-
2 persson 2423 * Copyright (C) 2006-2013 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    
50 schoenebeck 1225 class LabelWidget {
51     public:
52     Gtk::Label label;
53     Gtk::Widget& widget;
54    
55     LabelWidget(const char* labelText, Gtk::Widget& widget);
56     void set_sensitive(bool sensitive = true);
57 persson 1460 sigc::signal<void>& signal_value_changed() {
58 persson 1261 return sig_changed;
59     }
60 schoenebeck 1225 protected:
61 persson 2151 #ifdef OLD_TOOLTIPS
62 schoenebeck 1225 Gtk::Tooltips tooltips;
63 persson 2151 #endif
64 persson 1261 sigc::signal<void> sig_changed;
65 schoenebeck 1225 };
66    
67     class NumEntry : public LabelWidget {
68     protected:
69 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
70 schoenebeck 1225 Gtk::Adjustment adjust;
71 persson 2169 #else
72     Glib::RefPtr<Gtk::Adjustment> adjust;
73     #endif
74 schoenebeck 1225 Gtk::HScale scale;
75     Gtk::SpinButton spinbutton;
76     Gtk::HBox box;
77 persson 1261
78     int round_to_int(double x) {
79     return int(x < 0.0 ? x - 0.5 : x + 0.5);
80     }
81 schoenebeck 1225 public:
82     NumEntry(const char* labelText, double lower = 0, double upper = 127,
83     int decimals = 0);
84     void set_tip(const Glib::ustring& tip_text) {
85 persson 2151 #ifdef OLD_TOOLTIPS
86 schoenebeck 1225 tooltips.set_tip(spinbutton, tip_text);
87 persson 2151 #else
88     spinbutton.set_tooltip_text(tip_text);
89     #endif
90 schoenebeck 1225 }
91     void set_upper(double upper) {
92 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
93 schoenebeck 1225 adjust.set_upper(upper);
94 persson 2169 #else
95     adjust->set_upper(upper);
96     #endif
97 schoenebeck 1225 }
98     };
99    
100     class NumEntryGain : public NumEntry {
101     private:
102 persson 1460 int32_t value;
103 schoenebeck 1225 void value_changed();
104     double coeff;
105 persson 1460 bool connected;
106 schoenebeck 1225 public:
107     NumEntryGain(const char* labelText,
108     double lower, double upper, int decimals, double coeff);
109 persson 1460 int32_t get_value() const { return value; }
110     void set_value(int32_t value);
111 schoenebeck 1225 };
112    
113     template<typename T>
114     class NumEntryTemp : public NumEntry {
115     private:
116 persson 1460 T value;
117 schoenebeck 1225 void value_changed();
118     public:
119     NumEntryTemp(const char* labelText,
120     double lower = 0, double upper = 127, int decimals = 0);
121 persson 1460 T get_value() const { return value; }
122     void set_value(T value);
123 schoenebeck 1225 };
124    
125     template<typename T>
126     NumEntryTemp<T>::NumEntryTemp(const char* labelText,
127     double lower, double upper, int decimals) :
128 persson 1460 NumEntry(labelText, lower, upper, decimals),
129     value(0)
130 schoenebeck 1225 {
131     spinbutton.signal_value_changed().connect(
132     sigc::mem_fun(*this, &NumEntryTemp::value_changed));
133     }
134    
135     template<typename T>
136     void NumEntryTemp<T>::value_changed()
137     {
138 schoenebeck 1359 const double f = pow(10, spinbutton.get_digits());
139     int new_value = round_to_int(spinbutton.get_value() * f);
140 persson 1460 if (new_value != round_to_int(value * f)) {
141     value = T(new_value / f);
142     sig_changed();
143 schoenebeck 1225 }
144     }
145    
146     template<typename T>
147 persson 1460 void NumEntryTemp<T>::set_value(T value)
148 schoenebeck 1225 {
149 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
150 persson 1460 if (value > adjust.get_upper()) value = T(adjust.get_upper());
151 persson 2169 #else
152     if (value > adjust->get_upper()) value = T(adjust->get_upper());
153     #endif
154 persson 1460 if (this->value != value) {
155     this->value = value;
156     const double f = pow(10, spinbutton.get_digits());
157     if (round_to_int(spinbutton.get_value() * f) != round_to_int(value * f)) {
158     spinbutton.set_value(value);
159     }
160     sig_changed();
161     }
162 schoenebeck 1225 }
163    
164    
165     class NoteEntry : public NumEntryTemp<uint8_t> {
166     public:
167     NoteEntry(const char* labelText);
168     private:
169     int on_input(double* new_value);
170     bool on_output();
171     };
172    
173    
174     class NumEntryPermille : public NumEntry {
175     private:
176 persson 1460 uint16_t value;
177 schoenebeck 1225 void value_changed();
178     public:
179     NumEntryPermille(const char* labelText,
180     double lower = 0, double upper = 127, int decimals = 0);
181 persson 1460 uint16_t get_value() const { return value; }
182     void set_value(uint16_t value);
183 schoenebeck 1225 };
184    
185    
186     template<typename T>
187     class ChoiceEntry : public LabelWidget {
188     private:
189     Gtk::ComboBoxText combobox;
190     Gtk::Alignment align;
191     const T* values;
192     public:
193     ChoiceEntry(const char* labelText);
194 persson 1460 T get_value() const;
195     void set_value(T value);
196 schoenebeck 1225 void set_choices(const char** texts, const T* values);
197 persson 1460
198 schoenebeck 1225 void set_tip(const Glib::ustring& tip_text) {
199 persson 2151 #ifdef OLD_TOOLTIPS
200     tooltips.set_tip(combobox, tip_text);
201     #else
202     combobox.set_tooltip_text(tip_text);
203     #endif
204 schoenebeck 1225 }
205     };
206    
207     template<typename T>
208     ChoiceEntry<T>::ChoiceEntry(const char* labelText) :
209 persson 2151 LabelWidget(labelText, align),
210     align(0, 0, 0, 0)
211 schoenebeck 1225 {
212 persson 1460 combobox.signal_changed().connect(sig_changed.make_slot());
213 schoenebeck 1225 align.add(combobox);
214     }
215    
216     template<typename T>
217     void ChoiceEntry<T>::set_choices(const char** texts, const T* values)
218     {
219     for (int i = 0 ; texts[i] ; i++) {
220 persson 2169 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2
221 schoenebeck 1225 combobox.append_text(texts[i]);
222 persson 2169 #else
223     combobox.append(texts[i]);
224     #endif
225 schoenebeck 1225 }
226     this->values = values;
227     }
228    
229     template<typename T>
230 persson 1460 T ChoiceEntry<T>::get_value() const
231 schoenebeck 1225 {
232 schoenebeck 1359 int rowno = combobox.get_active_row_number();
233 persson 1460 return values[rowno];
234 schoenebeck 1225 }
235    
236     template<typename T>
237 persson 1460 void ChoiceEntry<T>::set_value(T value)
238 schoenebeck 1225 {
239 persson 1460 int row = 0;
240     int nb_rows = combobox.get_model()->children().size();
241     for (; row < nb_rows ; row++) {
242     if (value == values[row]) break;
243     }
244     combobox.set_active(row == nb_rows ? -1 : row);
245 schoenebeck 1225 }
246    
247    
248     class ChoiceEntryLeverageCtrl : public LabelWidget {
249     private:
250 persson 1460 gig::leverage_ctrl_t value;
251 schoenebeck 1225 Gtk::ComboBoxText combobox;
252     Gtk::Alignment align;
253     void value_changed();
254     public:
255     ChoiceEntryLeverageCtrl(const char* labelText);
256 persson 1460 gig::leverage_ctrl_t get_value() const { return value; }
257     void set_value(gig::leverage_ctrl_t value);
258 schoenebeck 1225 };
259    
260    
261     class BoolEntry : public LabelWidget {
262     private:
263     Gtk::CheckButton checkbutton;
264     public:
265     BoolEntry(const char* labelText);
266 persson 1460 bool get_value() const { return checkbutton.get_active(); }
267     void set_value(bool value) { checkbutton.set_active(value); }
268    
269 schoenebeck 1225 void set_tip(const Glib::ustring& tip_text) {
270 persson 2151 #ifdef OLD_TOOLTIPS
271 schoenebeck 1225 tooltips.set_tip(checkbutton, tip_text);
272 persson 2151 #else
273     checkbutton.set_tooltip_text(tip_text);
274     #endif
275 schoenebeck 1225 }
276     };
277    
278    
279     class BoolEntryPlus6 : public LabelWidget {
280     private:
281     Gtk::CheckButton checkbutton;
282     void value_changed();
283     NumEntryGain& eGain;
284     int32_t plus6value;
285     public:
286     BoolEntryPlus6(const char* labelText, NumEntryGain& eGain, int32_t plus6value);
287 persson 1460 int32_t get_value() const;
288     void set_value(int32_t value);
289 schoenebeck 1225 };
290    
291 persson 2446
292 schoenebeck 1225 class StringEntry : public LabelWidget {
293     private:
294     Gtk::Entry entry;
295     public:
296     StringEntry(const char* labelText);
297 persson 2446 gig::String get_value() const;
298     void set_value(const gig::String& value);
299 persson 1582 void set_width_chars(int n_chars) { entry.set_width_chars(n_chars); }
300 schoenebeck 1225 };
301    
302 persson 1582 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 persson 2446 void set_value(const gig::String& value);
311 persson 1582 };
312 schoenebeck 1225
313 persson 1582
314 persson 2423 /**
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 schoenebeck 1225 #endif

  ViewVC Help
Powered by ViewVC