~ubuntu-branches/ubuntu/feisty/photoprint/feisty

« back to all changes in this revision

Viewing changes to stpui_widgets/dimension.c

  • Committer: Bazaar Package Importer
  • Author(s): Milan Zamazal
  • Date: 2006-09-29 12:18:16 UTC
  • Revision ID: james.westby@ubuntu.com-20060929121816-6t2iz9zaymixd3om
Tags: upstream-0.3.3
ImportĀ upstreamĀ versionĀ 0.3.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * dimension.cpp - provides a custom widget for adjusting
 
3
 * a physical dimension
 
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.
 
7
 *
 
8
 */
 
9
 
 
10
#include <string.h>
 
11
 
 
12
#include <gtk/gtkframe.h>
 
13
#include <gtk/gtksizegroup.h>
 
14
#include <gtk/gtklabel.h>
 
15
 
 
16
#include <math.h>
 
17
#include "dimension.h"
 
18
 
 
19
enum {
 
20
        CHANGED_SIGNAL,
 
21
        LAST_SIGNAL
 
22
};
 
23
 
 
24
static guint dimension_signals[LAST_SIGNAL] = { 0 };
 
25
 
 
26
static void dimension_class_init (DimensionClass *klass);
 
27
static void dimension_init (Dimension *stpuicombo);
 
28
 
 
29
 
 
30
static void spin_changed(GtkWidget *wid,gpointer *ob)
 
31
{
 
32
        Dimension *lo=(Dimension *)ob;
 
33
//      GtkSpinButton *spin=GTK_SPIN_BUTTON(lo->spinbutton);
 
34
 
 
35
        g_signal_handlers_block_matched (G_OBJECT(lo->spinbutton),G_SIGNAL_MATCH_DATA,
 
36
                0, 0, NULL, NULL, lo);
 
37
 
 
38
        g_signal_emit(G_OBJECT (ob),dimension_signals[CHANGED_SIGNAL], 0);
 
39
 
 
40
        g_signal_handlers_unblock_matched (G_OBJECT(lo->spinbutton),G_SIGNAL_MATCH_DATA,
 
41
                0, 0, NULL, NULL, lo);
 
42
}
 
43
 
 
44
 
 
45
void dimension_refresh(Dimension *ob)
 
46
{
 
47
 
 
48
}
 
49
 
 
50
 
 
51
GtkWidget* dimension_new (gdouble min,gdouble max,enum Units unit)
 
52
{
 
53
        Dimension *ob=DIMENSION(g_object_new (dimension_get_type (), NULL));
 
54
        gtk_box_set_spacing(GTK_BOX(ob),5);
 
55
 
 
56
        ob->value=min;
 
57
        ob->minpt=min;
 
58
        ob->maxpt=max;
 
59
        ob->unit=UNIT_POINTS;
 
60
 
 
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);
 
66
 
 
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);
 
70
 
 
71
        dimension_set_unit(ob,unit);
 
72
        dimension_refresh(ob);
 
73
 
 
74
        return(GTK_WIDGET(ob));
 
75
}
 
76
 
 
77
 
 
78
GType
 
79
dimension_get_type (void)
 
80
{
 
81
        static GType stpuic_type = 0;
 
82
 
 
83
        if (!stpuic_type)
 
84
        {
 
85
                static const GTypeInfo dimension_info =
 
86
                {
 
87
                        sizeof (DimensionClass),
 
88
                        NULL, /* base_init */
 
89
                        NULL, /* base_finalize */
 
90
                        (GClassInitFunc) dimension_class_init,
 
91
                        NULL, /* class_finalize */
 
92
                        NULL, /* class_data */
 
93
                        sizeof (Dimension),
 
94
                        0,
 
95
                        (GInstanceInitFunc) dimension_init,
 
96
                };
 
97
                stpuic_type = g_type_register_static (GTK_TYPE_HBOX, "Dimension", &dimension_info, (GTypeFlags)0);
 
98
        }
 
99
        return stpuic_type;
 
100
}
 
101
 
 
102
 
 
103
static void
 
104
dimension_class_init (DimensionClass *klass)
 
105
{
 
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),
 
111
                NULL, NULL,
 
112
                g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
 
113
}
 
114
 
 
115
 
 
116
static void
 
117
dimension_init (Dimension *ob)
 
118
{
 
119
        ob->spinbutton=NULL;
 
120
        ob->label=NULL;
 
121
}
 
122
 
 
123
 
 
124
int dimension_get_pt(Dimension *ob)
 
125
{
 
126
        double v=gtk_spin_button_get_value(GTK_SPIN_BUTTON(ob->spinbutton));
 
127
        switch(ob->unit)
 
128
        {
 
129
                case UNIT_POINTS:
 
130
                        break;
 
131
                case UNIT_INCHES:
 
132
                        v=UNIT_ROUND_INCHES_TO_POINTS(v);
 
133
                        break;
 
134
                case UNIT_MILLIMETERS:
 
135
                        v=UNIT_ROUND_MILLIMETERS_TO_POINTS(v);
 
136
                        break;
 
137
                case UNIT_CENTIMETERS:
 
138
                        v=UNIT_ROUND_CENTIMETERS_TO_POINTS(v);
 
139
                        break;
 
140
        }
 
141
        ob->value=v;
 
142
        return(v);
 
143
}
 
144
 
 
145
 
 
146
void dimension_update_unit_label(Dimension *ob)
 
147
{
 
148
        char *txt=NULL;
 
149
        switch(ob->unit)
 
150
        {
 
151
                case UNIT_POINTS:
 
152
                        txt="pt";
 
153
                        break;
 
154
                case UNIT_INCHES:
 
155
                        txt="in";
 
156
                        break;
 
157
                case UNIT_MILLIMETERS:
 
158
                        txt="mm";
 
159
                        break;
 
160
                case UNIT_CENTIMETERS:
 
161
                        txt="cm";
 
162
                        break;
 
163
        }
 
164
        gtk_label_set_text(GTK_LABEL(ob->label),txt);
 
165
}
 
166
 
 
167
 
 
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};
 
171
 
 
172
void sanitycheck()
 
173
{
 
174
        int i;
 
175
        double r;
 
176
 
 
177
        for(i=0;i<(sizeof(mmtest)/sizeof(float));++i)
 
178
        {
 
179
                r=UNIT_MILLIMETERS_TO_POINTS(mmtest[i]);
 
180
                printf("%lfmm -> %lfpt ",mmtest[i],r);
 
181
                printf("(%lfmm)\n",ROUNDTONEAREST(r,POINTS_PRECISION));
 
182
        }
 
183
        printf("\n");
 
184
        for(i=0;i<(sizeof(intest)/sizeof(float));++i)
 
185
        {
 
186
                r=UNIT_INCHES_TO_POINTS(intest[i]);
 
187
                printf("%lfin -> %lfpt ",intest[i],r);
 
188
                printf("(%lfin)\n",ROUNDTONEAREST(r,INCHES_PRECISION));
 
189
        }
 
190
        printf("\n");
 
191
        for(i=0;i<(sizeof(pttest)/sizeof(int));++i)
 
192
        {
 
193
                r=UNIT_POINTS_TO_MILLIMETERS(pttest[i]);
 
194
                printf("%dpt -> %lfmm ",pttest[i],r);
 
195
                printf("(%lfin)\n",ROUNDTONEAREST(r,MILLIMETERS_PRECISION));
 
196
        }
 
197
}
 
198
 
 
199
 
 
200
void dimension_set_unit(Dimension *ob,enum Units unit)
 
201
{
 
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);
 
206
        switch(ob->unit)
 
207
        {
 
208
                case UNIT_POINTS:
 
209
                        break;
 
210
                case UNIT_INCHES:
 
211
                        v=UNIT_ROUND_INCHES_TO_POINTS(v);
 
212
                        break;
 
213
                case UNIT_MILLIMETERS:
 
214
                        v=UNIT_ROUND_MILLIMETERS_TO_POINTS(v);
 
215
                        break;
 
216
                case UNIT_CENTIMETERS:
 
217
                        v=UNIT_ROUND_CENTIMETERS_TO_POINTS(v);
 
218
                        break;
 
219
                default:
 
220
                        fprintf(stderr,"PANIC! Unit %d was not caught!\n",ob->unit);
 
221
                        break;
 
222
        }
 
223
        ob->unit=unit;
 
224
        switch(ob->unit)
 
225
        {
 
226
                case UNIT_POINTS:
 
227
                        min=ob->minpt;
 
228
                        max=ob->maxpt;
 
229
                        step=POINTS_PRECISION;
 
230
                        break;
 
231
                case UNIT_INCHES:
 
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;
 
236
                        break;
 
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;
 
242
                        break;
 
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;
 
248
                        break;
 
249
                default:
 
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);
 
252
                        break;
 
253
        }
 
254
 
 
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");
 
257
//      sanitycheck();
 
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);
 
262
}
 
263
 
 
264
 
 
265
void dimension_set_pt(Dimension *ob,int pt)
 
266
{
 
267
        double v=0.0;
 
268
        if(pt==ob->value)
 
269
                return;
 
270
 
 
271
        switch(ob->unit)
 
272
        {
 
273
                case UNIT_POINTS:
 
274
                        v=pt;
 
275
                        break;
 
276
                case UNIT_INCHES:
 
277
                        v=UNIT_ROUND_POINTS_TO_INCHES(pt);
 
278
                        break;
 
279
                case UNIT_MILLIMETERS:
 
280
                        v=UNIT_ROUND_POINTS_TO_MILLIMETERS(pt);
 
281
                        break;
 
282
                case UNIT_CENTIMETERS:
 
283
                        v=UNIT_ROUND_POINTS_TO_CENTIMETERS(pt);
 
284
                        break;
 
285
        }
 
286
 
 
287
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(ob->spinbutton),v);
 
288
        ob->value=v;
 
289
}
 
290
 
 
291
 
 
292
void dimension_set_value(Dimension *ob,gdouble val,enum Units unit)
 
293
{
 
294
        dimension_set_unit(ob,unit);
 
295
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(ob->spinbutton),val);
 
296
}
 
297
 
 
298
 
 
299
void dimension_set_range_pt(Dimension *ob,int low,int high)
 
300
{
 
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);
 
304
}
 
305