2
* dimension.cpp - provides a custom widget for adjusting
4
* Copyright (c) 2004 by Alastair M. Robinson
5
* Distributed under the terms of the GNU General Public License -
6
* see the file named "COPYING" for more details.
12
#include <gtk/gtkframe.h>
13
#include <gtk/gtksizegroup.h>
14
#include <gtk/gtklabel.h>
17
#include "dimension.h"
24
static guint dimension_signals[LAST_SIGNAL] = { 0 };
26
static void dimension_class_init (DimensionClass *klass);
27
static void dimension_init (Dimension *stpuicombo);
30
static void spin_changed(GtkWidget *wid,gpointer *ob)
32
Dimension *lo=(Dimension *)ob;
33
// GtkSpinButton *spin=GTK_SPIN_BUTTON(lo->spinbutton);
35
g_signal_handlers_block_matched (G_OBJECT(lo->spinbutton),G_SIGNAL_MATCH_DATA,
36
0, 0, NULL, NULL, lo);
38
g_signal_emit(G_OBJECT (ob),dimension_signals[CHANGED_SIGNAL], 0);
40
g_signal_handlers_unblock_matched (G_OBJECT(lo->spinbutton),G_SIGNAL_MATCH_DATA,
41
0, 0, NULL, NULL, lo);
45
void dimension_refresh(Dimension *ob)
51
GtkWidget* dimension_new (gdouble min,gdouble max,enum Units unit)
53
Dimension *ob=DIMENSION(g_object_new (dimension_get_type (), NULL));
54
gtk_box_set_spacing(GTK_BOX(ob),5);
61
ob->spinbutton=gtk_spin_button_new_with_range(min,max,0.01);
62
gtk_spin_button_set_value(GTK_SPIN_BUTTON(ob->spinbutton),ob->value);
63
gtk_box_pack_start(GTK_BOX(ob),ob->spinbutton,TRUE,TRUE,0);
64
g_signal_connect(G_OBJECT(ob->spinbutton),"value-changed",G_CALLBACK(spin_changed),ob);
65
gtk_widget_show(ob->spinbutton);
67
ob->label=gtk_label_new(NULL);
68
gtk_box_pack_start(GTK_BOX(ob),ob->label,TRUE,TRUE,0);
69
gtk_widget_show(ob->label);
71
dimension_set_unit(ob,unit);
72
dimension_refresh(ob);
74
return(GTK_WIDGET(ob));
79
dimension_get_type (void)
81
static GType stpuic_type = 0;
85
static const GTypeInfo dimension_info =
87
sizeof (DimensionClass),
89
NULL, /* base_finalize */
90
(GClassInitFunc) dimension_class_init,
91
NULL, /* class_finalize */
92
NULL, /* class_data */
95
(GInstanceInitFunc) dimension_init,
97
stpuic_type = g_type_register_static (GTK_TYPE_HBOX, "Dimension", &dimension_info, (GTypeFlags)0);
104
dimension_class_init (DimensionClass *klass)
106
dimension_signals[CHANGED_SIGNAL] =
107
g_signal_new ("value-changed",
108
G_TYPE_FROM_CLASS (klass),
109
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
110
G_STRUCT_OFFSET (DimensionClass, changed),
112
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
117
dimension_init (Dimension *ob)
124
int dimension_get_pt(Dimension *ob)
126
double v=gtk_spin_button_get_value(GTK_SPIN_BUTTON(ob->spinbutton));
132
v=UNIT_ROUND_INCHES_TO_POINTS(v);
134
case UNIT_MILLIMETERS:
135
v=UNIT_ROUND_MILLIMETERS_TO_POINTS(v);
137
case UNIT_CENTIMETERS:
138
v=UNIT_ROUND_CENTIMETERS_TO_POINTS(v);
146
void dimension_update_unit_label(Dimension *ob)
157
case UNIT_MILLIMETERS:
160
case UNIT_CENTIMETERS:
164
gtk_label_set_text(GTK_LABEL(ob->label),txt);
168
double mmtest[]={0.0,1.0,5.5,10.5};
169
double intest[]={0.0,1.0,5.5,10.5};
170
int pttest[]={0,1,50,72,75,100};
177
for(i=0;i<(sizeof(mmtest)/sizeof(float));++i)
179
r=UNIT_MILLIMETERS_TO_POINTS(mmtest[i]);
180
printf("%lfmm -> %lfpt ",mmtest[i],r);
181
printf("(%lfmm)\n",ROUNDTONEAREST(r,POINTS_PRECISION));
184
for(i=0;i<(sizeof(intest)/sizeof(float));++i)
186
r=UNIT_INCHES_TO_POINTS(intest[i]);
187
printf("%lfin -> %lfpt ",intest[i],r);
188
printf("(%lfin)\n",ROUNDTONEAREST(r,INCHES_PRECISION));
191
for(i=0;i<(sizeof(pttest)/sizeof(int));++i)
193
r=UNIT_POINTS_TO_MILLIMETERS(pttest[i]);
194
printf("%dpt -> %lfmm ",pttest[i],r);
195
printf("(%lfin)\n",ROUNDTONEAREST(r,MILLIMETERS_PRECISION));
200
void dimension_set_unit(Dimension *ob,enum Units unit)
202
gdouble v=gtk_spin_button_get_value(GTK_SPIN_BUTTON(ob->spinbutton));
203
gdouble min=-1,max=-1,step=-1;
204
// fprintf(stderr,"Initial value (unit %d): %f\n",ob->unit,v);
205
// fprintf(stderr,"Initial range (pts): %f -> %f\n",ob->minpt,ob->maxpt);
211
v=UNIT_ROUND_INCHES_TO_POINTS(v);
213
case UNIT_MILLIMETERS:
214
v=UNIT_ROUND_MILLIMETERS_TO_POINTS(v);
216
case UNIT_CENTIMETERS:
217
v=UNIT_ROUND_CENTIMETERS_TO_POINTS(v);
220
fprintf(stderr,"PANIC! Unit %d was not caught!\n",ob->unit);
229
step=POINTS_PRECISION;
232
v=UNIT_ROUND_POINTS_TO_INCHES(v);
233
min=UNIT_ROUND_POINTS_TO_INCHES(ob->minpt);
234
max=UNIT_ROUND_POINTS_TO_INCHES(ob->maxpt);
235
step=INCHES_PRECISION;
237
case UNIT_MILLIMETERS:
238
v=UNIT_ROUND_POINTS_TO_MILLIMETERS(v);
239
min=UNIT_ROUND_POINTS_TO_MILLIMETERS(ob->minpt);
240
max=UNIT_ROUND_POINTS_TO_MILLIMETERS(ob->maxpt);
241
step=MILLIMETERS_PRECISION;
243
case UNIT_CENTIMETERS:
244
v=UNIT_ROUND_POINTS_TO_CENTIMETERS(v);
245
min=UNIT_ROUND_POINTS_TO_CENTIMETERS(ob->minpt);
246
max=UNIT_ROUND_POINTS_TO_CENTIMETERS(ob->maxpt);
247
step=CENTIMETERS_PRECISION;
250
fprintf(stderr,"PANIC! Unit %d was not caught!\n",ob->unit);
251
fprintf(stderr,"Unit definitions: %d, %d, %d, %d\n",UNIT_POINTS,UNIT_INCHES,UNIT_MILLIMETERS,UNIT_CENTIMETERS);
255
// fprintf(stderr,"Setting range to: %f, %f; value to %f; step to %f (unit %d)\n",min,max,v,step,ob->unit);
256
// fprintf(stderr,"Sanity check:\n");
258
gtk_spin_button_set_range(GTK_SPIN_BUTTON(ob->spinbutton),min,max);
259
gtk_spin_button_set_value(GTK_SPIN_BUTTON(ob->spinbutton),v);
260
gtk_spin_button_set_increments(GTK_SPIN_BUTTON(ob->spinbutton),step,step*10.0);
261
dimension_update_unit_label(ob);
265
void dimension_set_pt(Dimension *ob,int pt)
277
v=UNIT_ROUND_POINTS_TO_INCHES(pt);
279
case UNIT_MILLIMETERS:
280
v=UNIT_ROUND_POINTS_TO_MILLIMETERS(pt);
282
case UNIT_CENTIMETERS:
283
v=UNIT_ROUND_POINTS_TO_CENTIMETERS(pt);
287
gtk_spin_button_set_value(GTK_SPIN_BUTTON(ob->spinbutton),v);
292
void dimension_set_value(Dimension *ob,gdouble val,enum Units unit)
294
dimension_set_unit(ob,unit);
295
gtk_spin_button_set_value(GTK_SPIN_BUTTON(ob->spinbutton),val);
299
void dimension_set_range_pt(Dimension *ob,int low,int high)
301
// FIXME - this needs to convert to the current unit
302
// and update low/high in class.
303
gtk_spin_button_set_range(GTK_SPIN_BUTTON(ob->spinbutton),low,high);