gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Scales.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef SCALE_H_
18 #define SCALE_H_
19 
20 #include "Widget.H"
21 #include <mffm/LinkList.H>
22 #include <stdlib.h>
23 #ifdef _MSC_VER
24 #define _USE_MATH_DEFINES
25 #endif
26 #include <math.h>
27 #include <limits>
28 
29 #ifdef _MSC_VER
30 #ifndef MS_ROUND
31 double round(double number) {
33  return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);
34 }
35 #else
36 extern double round(double);
37 #endif
38 #endif
39 
40 #ifndef SCALE_MAX_DISPLAY_MAX_PRECISION
41 #define SCALE_MAX_DISPLAY_MAX_PRECISION 37
42 #endif
43 
47 public:
55  ScaleFuncData(double mi, double ma, double st, void (*fn)(void *, void *), gpointer da) {
56  min=mi;
57  max=ma;
58  step=st;
59  func=fn;
60  data=da;
61  }
69  ScaleFuncData(double mi, double ma, double st, GCallback fn, gpointer da) {
70  min=mi;
71  max=ma;
72  step=st;
73  func=(void (*)(void*, void*))fn;
74  data=da;
75  }
76 
77  double min;
78  double max;
79  double step;
80  void (*func)(void *arg1, void *arg2);
81  gpointer data;
82 };
83 #define SCALESTRUCT(mi,ma,st,fn,da) ScaleFuncData(mi,ma,st,fn,da)
84 
87 class HScaleFuncData : public ScaleFuncData {
88 public:
94  HScaleFuncData(double mi, double ma, double st) : ScaleFuncData(mi, ma, st, (GCallback)NULL, NULL) {}
102  HScaleFuncData(double mi, double ma, double st, void (*fn)(void *, void *),gpointer da) : ScaleFuncData(mi, ma, st, fn, da) {}
110  HScaleFuncData(double mi, double ma, double st, GCallback fn, gpointer da) : ScaleFuncData(mi, ma, st, fn, da) {}
111 };
112 
116 public:
122  VScaleFuncData(double mi, double ma, double st) : ScaleFuncData(mi, ma, st, (GCallback)NULL, NULL) {}
130  VScaleFuncData(double mi, double ma, double st, void (*fn)(void *, void *),gpointer da) : ScaleFuncData(mi, ma, st, fn, da) {}
138  VScaleFuncData(double mi, double ma, double st, GCallback fn, gpointer da) : ScaleFuncData(mi, ma, st, fn, da) {}
139 };
140 
159 class Scales : public LinkList<GtkWidget *> {
160 
164  void setDigits(double step) {
165  step=fabs(step)-floor(fabs(step))/10.; // find the remainder mod 1.
166  int digits=1; // start with no digits
167  if (step!=0.) // if the remainder is < 0. then we need more digits.
168  while ((step*=10.)<1.) // keep adding digits till we get above 1. and that is the number we need
169  digits++;
170  if (digits>SCALE_MAX_DISPLAY_MAX_PRECISION) digits=SCALE_MAX_DISPLAY_MAX_PRECISION; // ensure we don't show too much
171  gtk_scale_set_digits(GTK_SCALE(current()),digits);
172  }
173 
178  static void quantiseVal(void *wid, void *data) {
179  GtkAdjustment *adj=gtk_range_get_adjustment(GTK_RANGE(wid));
180 #if GTK_MAJOR_VERSION!=2
181  double step_size=gtk_adjustment_get_step_increment(adj);
182 #else
183  double step_size=adj->step_increment;
184 #endif
185  int rounded=(int)round(gtk_range_get_value(GTK_RANGE(wid))/step_size);
186  gtk_range_set_value(GTK_RANGE(wid),step_size*(double)rounded);
187  }
188 
189 public:
191  virtual ~Scales(void) {
192  }
193 
198  static double getVal(GtkRange * wid) {
199  return gtk_range_get_value(wid);
200  }
201 
205  double getVal(void) {
206  if (getCount())
207  return gtk_range_get_value((GtkRange*)current());
208  return std::numeric_limits<double>::quiet_NaN();
209  }
210 
214  double getStep(void) {
215  if (getCount()) {
216  GtkAdjustment* gtkAdj=gtk_range_get_adjustment(GTK_RANGE(current()));
217 #if GTK_MAJOR_VERSION!=2
218  return gtk_adjustment_get_step_increment(gtkAdj);
219 #else
220  return gtkAdj->step_increment;
221 #endif
222  }
223  return std::numeric_limits<double>::quiet_NaN();
224  }
225 
229  void setStep(double step) {
230  if (getCount()) {
231  GtkAdjustment* gtkAdj=gtk_range_get_adjustment(GTK_RANGE(current()));
232 #if GTK_MAJOR_VERSION!=2
233  return gtk_adjustment_set_step_increment(gtkAdj, step);
234 #else
235  gtkAdj->step_increment=step;
236 #endif
237  gtk_range_set_adjustment(GTK_RANGE(current()), gtkAdj);
238  }
239  }
240 
244  void setLower(double lower){
245  if (getCount()) {
246  GtkAdjustment* gtkAdj=gtk_range_get_adjustment(GTK_RANGE(current()));
247 #if GTK_MAJOR_VERSION!=2
248  gtk_adjustment_set_lower(gtkAdj, lower);
249 #else
250  gtkAdj->lower=lower;
251 #endif
252  gtk_range_set_adjustment(GTK_RANGE(current()), gtkAdj);
253  }
254  }
255 
256 
260  void setUpper(double upper){
261  if (getCount()) {
262  GtkAdjustment* gtkAdj=gtk_range_get_adjustment(GTK_RANGE(current()));
263 #if GTK_MAJOR_VERSION!=2
264  gtk_adjustment_set_upper(gtkAdj, upper);
265 #else
266  gtkAdj->upper=upper;
267 #endif
268  gtk_range_set_adjustment(GTK_RANGE(current()), gtkAdj);
269  }
270  }
271 
275  double getLower(void){
276  if (getCount()) {
277  GtkAdjustment* gtkAdj=gtk_range_get_adjustment(GTK_RANGE(current()));
278 #if GTK_MAJOR_VERSION!=2
279  return gtk_adjustment_get_lower(gtkAdj);
280 #else
281  return gtkAdj->lower;
282 #endif
283  }
284  }
285 
289  double getUpper(void){
290  if (getCount()) {
291  GtkAdjustment* gtkAdj=gtk_range_get_adjustment(GTK_RANGE(current()));
292 #if GTK_MAJOR_VERSION!=2
293  return gtk_adjustment_get_upper(gtkAdj);
294 #else
295  return gtkAdj->upper;
296 #endif
297  }
298  }
299 
304  Scales& operator =(double pos) {
305  if (getCount())
306  gtk_range_set_value((GtkRange*)current(),pos);
307  return *this;
308  }
309 
314  Scales& operator =(int pos) {
315  return this->operator=((double)pos);
316  }
317 
322  if (getCount())
323  gtk_scale_set_draw_value((GtkScale*)current(), FALSE);
324  }
325 
331  LinkList<GtkWidget *>::add(gtk_hscale_new_with_range(sf.min,sf.max,sf.step));
332  setDigits(sf.step);
333  gtk_widget_show(current());
334  if (sf.func) //set the callback
335  g_signal_connect(G_OBJECT((GtkScale*)current()), "value_changed",reinterpret_cast<GCallback>(sf.func), sf.data);
336  return *this;
337  }
338 
344  LinkList<GtkWidget *>::add(gtk_vscale_new_with_range(sf.min,sf.max,sf.step));
345  setDigits(sf.step);
346  gtk_widget_show(current());
347  if (sf.func) //set the callback
348  g_signal_connect(G_OBJECT((GtkScale*)current()), "value_changed",reinterpret_cast<GCallback>(sf.func), sf.data);
349  return *this;
350  }
351 
356  int findIndex(GtkWidget* widget) {
357  int ret=0;
358  if (getCount()) {
359  GtkWidget *c=current(); // remember our current location
360  grab(1);
361  prev(); // we are at the last on the list
362  for (int i=1; i<=getCount(); i++) {
363  if (next()==widget) {
364  ret=i;
365  break;
366  }
367  }
368  while (next()!=c) ; // return to the original button
369  }
370  return ret;
371  }
372 
377  if (sfd.func)
378  g_signal_connect_after(G_OBJECT(current()), "value_changed",reinterpret_cast<GCallback>(sfd.func), sfd.data);
379  }
380 
385  if (sfd.func) {
386  g_signal_handlers_disconnect_by_func(G_OBJECT(current()), (void*)(sfd.func), sfd.data);
387  }
388  }
389 
390 
394  void quantise(bool isQuantised) {
395  if (getCount())
396  if (isQuantised)
397  g_signal_connect(G_OBJECT(current()), "value_changed",reinterpret_cast<GCallback>(quantiseVal), NULL);
398  else
399  g_signal_handlers_disconnect_matched(G_OBJECT((GtkButton*)current()), G_SIGNAL_MATCH_FUNC, 0, 0, NULL, reinterpret_cast<gpointer>(quantiseVal), NULL);
400  }
401 
405  void invert(bool isInverted) {
406  if (getCount())
407  gtk_range_set_inverted(GTK_RANGE(current()),isInverted);
408  }
409 };
410 
411 #endif //SCALE_H_
void setStep(double step)
Definition: Scales.H:229
virtual ~Scales(void)
destructor
Definition: Scales.H:191
VScaleFuncData(double mi, double ma, double st)
Definition: Scales.H:122
static void quantiseVal(void *wid, void *data)
Definition: Scales.H:178
double max
The maximum value.
Definition: Scales.H:78
void setUpper(double upper)
Definition: Scales.H:260
VScaleFuncData(double mi, double ma, double st, GCallback fn, gpointer da)
Definition: Scales.H:138
void signalConnectAfter(ScaleFuncData sfd)
connect a callback to execute after the currently connected callbacks
Definition: Scales.H:376
VScaleFuncData(double mi, double ma, double st, void(*fn)(void *, void *), gpointer da)
Definition: Scales.H:130
int findIndex(GtkWidget *widget)
Definition: Scales.H:356
Scales & drawValue()
Definition: Scales.H:321
double step
The stepping value.
Definition: Scales.H:79
double getUpper(void)
Definition: Scales.H:289
void setDigits(double step)
Definition: Scales.H:164
HScaleFuncData(double mi, double ma, double st)
Definition: Scales.H:94
HScaleFuncData(double mi, double ma, double st, GCallback fn, gpointer da)
Definition: Scales.H:110
gpointer data
The user data to pass to the callback function.
Definition: Scales.H:81
HScaleFuncData(double mi, double ma, double st, void(*fn)(void *, void *), gpointer da)
Definition: Scales.H:102
double precision function round(x)
Definition: round.f:2
double getLower(void)
Definition: Scales.H:275
double getStep(void)
Definition: Scales.H:214
ScaleFuncData(double mi, double ma, double st, void(*fn)(void *, void *), gpointer da)
Definition: Scales.H:55
void invert(bool isInverted)
Definition: Scales.H:405
#define SCALE_MAX_DISPLAY_MAX_PRECISION
the maximum precision we want to show
Definition: Scales.H:41
void(* func)(void *arg1, void *arg2)
The callback function.
Definition: Scales.H:80
void setLower(double lower)
Definition: Scales.H:244
double min
The minimum value.
Definition: Scales.H:77
Scales & operator<<(HScaleFuncData sf)
Definition: Scales.H:330
Definition: Scales.H:159
void quantise(bool isQuantised)
Definition: Scales.H:394
ScaleFuncData(double mi, double ma, double st, GCallback fn, gpointer da)
Definition: Scales.H:69
void signalRemove(ScaleFuncData sfd)
disconnect the callback
Definition: Scales.H:384
double getVal(void)
Definition: Scales.H:205
static double getVal(GtkRange *wid)
Definition: Scales.H:198
Scales & operator<<(VScaleFuncData sf)
Definition: Scales.H:343
gtkIOStream: /tmp/gtkiostream/include/Scales.H Source File
GTK+ IOStream  Beta