]> git.sur5r.net Git - glabels/blob - glabels2/src/wdgt-position.c
Initial revision
[glabels] / glabels2 / src / wdgt-position.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  wdgt_position.c:  position properties widget module
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <config.h>
24
25 #include "wdgt-position.h"
26 #include "prefs.h"
27 #include "marshal.h"
28
29 #include "debug.h"
30
31 /*===========================================*/
32 /* Private types                             */
33 /*===========================================*/
34
35 enum {
36         CHANGED,
37         LAST_SIGNAL
38 };
39
40 typedef void (*glWdgtPositionSignal) (GObject * object, gpointer data);
41
42 /*===========================================*/
43 /* Private globals                           */
44 /*===========================================*/
45
46 static GObjectClass *parent_class;
47
48 static gint wdgt_position_signals[LAST_SIGNAL] = { 0 };
49 \f
50 /*===========================================*/
51 /* Local function prototypes                 */
52 /*===========================================*/
53
54 static void gl_wdgt_position_class_init (glWdgtPositionClass * class);
55 static void gl_wdgt_position_instance_init (glWdgtPosition * position);
56 static void gl_wdgt_position_finalize (GObject * object);
57 static void gl_wdgt_position_construct (glWdgtPosition * position,
58                                         gchar * label);
59 static void changed_cb (glWdgtPosition * position);
60 \f
61 /*================================================================*/
62 /* Boilerplate Object stuff.                                      */
63 /*================================================================*/
64 guint
65 gl_wdgt_position_get_type (void)
66 {
67         static guint wdgt_position_type = 0;
68
69         if (!wdgt_position_type) {
70                 GTypeInfo wdgt_position_info = {
71                         sizeof (glWdgtPositionClass),
72                         NULL,
73                         NULL,
74                         (GClassInitFunc) gl_wdgt_position_class_init,
75                         NULL,
76                         NULL,
77                         sizeof (glWdgtPosition),
78                         0,
79                         (GInstanceInitFunc) gl_wdgt_position_instance_init,
80                 };
81
82                 wdgt_position_type = g_type_register_static (gtk_vbox_get_type (),
83                                                              "glWdgtPosition",
84                                                              &wdgt_position_info,
85                                                              0);
86         }
87
88         return wdgt_position_type;
89 }
90
91 static void
92 gl_wdgt_position_class_init (glWdgtPositionClass * class)
93 {
94         GObjectClass *object_class;
95
96         object_class = (GObjectClass *) class;
97
98         parent_class = gtk_type_class (gtk_vbox_get_type ());
99
100         object_class->finalize = gl_wdgt_position_finalize;
101
102         wdgt_position_signals[CHANGED] =
103             g_signal_new ("changed",
104                           G_OBJECT_CLASS_TYPE(object_class),
105                           G_SIGNAL_RUN_LAST,
106                           G_STRUCT_OFFSET (glWdgtPositionClass, changed),
107                           NULL, NULL,
108                           gl_marshal_VOID__VOID,
109                           G_TYPE_NONE, 0);
110
111 }
112
113 static void
114 gl_wdgt_position_instance_init (glWdgtPosition * position)
115 {
116         position->x_spin = NULL;
117         position->y_spin = NULL;
118 }
119
120 static void
121 gl_wdgt_position_finalize (GObject * object)
122 {
123         glWdgtPosition *position;
124
125         g_return_if_fail (object != NULL);
126         g_return_if_fail (GL_IS_WDGT_POSITION (object));
127
128         position = GL_WDGT_POSITION (object);
129
130         G_OBJECT_CLASS (parent_class)->finalize (object);
131 }
132
133 GtkWidget *
134 gl_wdgt_position_new (gchar * label)
135 {
136         glWdgtPosition *position;
137
138         position = g_object_new (gl_wdgt_position_get_type (), NULL);
139
140         gl_wdgt_position_construct (position, label);
141
142         return GTK_WIDGET (position);
143 }
144 \f
145 /*============================================================*/
146 /* Construct composite widget.                                */
147 /*============================================================*/
148 static void
149 gl_wdgt_position_construct (glWdgtPosition * position,
150                             gchar * label)
151 {
152         GtkWidget *wvbox, *wframe, *wtable, *wlabel;
153         GtkObject *x_adjust, *y_adjust;
154         const gchar *units_string;
155         gdouble units_per_point, climb_rate;
156         gint digits;
157
158         gl_debug (DEBUG_WDGT, "START");
159
160         units_string = gl_prefs_get_units_string ();
161         units_per_point = gl_prefs_get_units_per_point ();
162         climb_rate = gl_prefs_get_units_step_size ();
163         digits = gl_prefs_get_units_precision ();
164
165         gl_debug (DEBUG_WDGT, "units = %s", units_string);
166         gl_debug (DEBUG_WDGT, "units/point = %f", units_per_point);
167         gl_debug (DEBUG_WDGT, "climb_rate = %f", climb_rate);
168         gl_debug (DEBUG_WDGT, "digits = %d", digits);
169
170         wvbox = GTK_WIDGET (position);
171
172         wframe = gtk_frame_new (label);
173         gtk_box_pack_start (GTK_BOX (wvbox), wframe, FALSE, FALSE, 0);
174
175         wtable = gtk_table_new (2, 3, TRUE);
176         gtk_container_set_border_width (GTK_CONTAINER (wtable), 10);
177         gtk_table_set_row_spacings (GTK_TABLE (wtable), 5);
178         gtk_table_set_col_spacings (GTK_TABLE (wtable), 5);
179         gtk_container_add (GTK_CONTAINER (wframe), wtable);
180
181         /* X label */
182         wlabel = gtk_label_new (_("X:"));
183         gtk_misc_set_alignment (GTK_MISC (wlabel), 0, 0.5);
184         gtk_label_set_justify (GTK_LABEL (wlabel), GTK_JUSTIFY_RIGHT);
185         gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 0, 1);
186         /* X spin */
187         x_adjust = gtk_adjustment_new (0.0, 0.0, 100.0, climb_rate, 10.0, 10.0);
188         position->x_spin = gtk_spin_button_new (GTK_ADJUSTMENT (x_adjust),
189                                                 climb_rate, digits);
190         gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (position->x_spin),
191                                            TRUE);
192         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (position->x_spin), TRUE);
193         g_signal_connect_swapped (G_OBJECT (position->x_spin), "changed",
194                                   G_CALLBACK (changed_cb),
195                                   G_OBJECT (position));
196         gtk_table_attach_defaults (GTK_TABLE (wtable), position->x_spin,
197                                    1, 2, 0, 1);
198
199         /* Y label */
200         wlabel = gtk_label_new (_("Y:"));
201         gtk_misc_set_alignment (GTK_MISC (wlabel), 0, 0.5);
202         gtk_label_set_justify (GTK_LABEL (wlabel), GTK_JUSTIFY_RIGHT);
203         gtk_table_attach_defaults (GTK_TABLE (wtable), wlabel, 0, 1, 1, 2);
204         /* Y spin */
205         y_adjust = gtk_adjustment_new (0.0, 0.0, 100.0, climb_rate, 10.0, 10.0);
206         position->y_spin = gtk_spin_button_new (GTK_ADJUSTMENT (y_adjust),
207                                                 climb_rate, digits);
208         gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (position->y_spin),
209                                            TRUE);
210         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (position->y_spin), TRUE);
211         g_signal_connect_swapped (G_OBJECT (position->y_spin), "changed",
212                                   G_CALLBACK (changed_cb),
213                                   G_OBJECT (position));
214         gtk_table_attach_defaults (GTK_TABLE (wtable), position->y_spin,
215                                    1, 2, 1, 2);
216
217         /* Units */
218         position->units_label = gtk_label_new (units_string);
219         gtk_misc_set_alignment (GTK_MISC (position->units_label), 0, 0.5);
220         gtk_table_attach_defaults (GTK_TABLE (wtable),
221                                    position->units_label, 2, 3, 1, 2);
222
223         gl_debug (DEBUG_WDGT, "END");
224 }
225
226 /*--------------------------------------------------------------------------*/
227 /* PRIVATE.  Callback for when any control in the widget has changed.       */
228 /*--------------------------------------------------------------------------*/
229 static void
230 changed_cb (glWdgtPosition * position)
231 {
232         /* Emit our "changed" signal */
233         g_signal_emit (G_OBJECT (position), wdgt_position_signals[CHANGED], 0);
234 }
235 \f
236 /*====================================================================*/
237 /* query values from controls.                                        */
238 /*====================================================================*/
239 void
240 gl_wdgt_position_get_position (glWdgtPosition * position,
241                                gdouble * x,
242                                gdouble * y)
243 {
244         gdouble units_per_point;
245
246         units_per_point = gl_prefs_get_units_per_point ();
247
248         *x = gtk_spin_button_get_value (GTK_SPIN_BUTTON(position->x_spin));
249         *y = gtk_spin_button_get_value (GTK_SPIN_BUTTON(position->y_spin));
250
251         /* convert everything back to our internal units (points) */
252         *x /= units_per_point;
253         *y /= units_per_point;
254 }
255
256 /*====================================================================*/
257 /* fill in values and ranges for controls.                            */
258 /*====================================================================*/
259 void
260 gl_wdgt_position_set_params (glWdgtPosition * position,
261                              gdouble x,
262                              gdouble y,
263                              gdouble x_max,
264                              gdouble y_max)
265 {
266         GtkObject *x_adjust, *y_adjust;
267         const gchar *units_string;
268         gdouble units_per_point, climb_rate;
269         gint digits;
270
271         units_string = gl_prefs_get_units_string ();
272         units_per_point = gl_prefs_get_units_per_point ();
273         climb_rate = gl_prefs_get_units_step_size ();
274         digits = gl_prefs_get_units_precision ();
275
276         /* Put everything into our display units */
277         x *= units_per_point;
278         y *= units_per_point;
279         x_max *= units_per_point;
280         y_max *= units_per_point;
281
282         /* update X/Y spin controls */
283         x_adjust = gtk_adjustment_new (x, 0.0, x_max, climb_rate, 10.0, 10.0);
284         gtk_spin_button_configure (GTK_SPIN_BUTTON (position->x_spin),
285                                    GTK_ADJUSTMENT (x_adjust), climb_rate,
286                                    digits);
287         y_adjust = gtk_adjustment_new (y, 0.0, y_max, climb_rate, 10.0, 10.0);
288         gtk_spin_button_configure (GTK_SPIN_BUTTON (position->y_spin),
289                                    GTK_ADJUSTMENT (y_adjust), climb_rate,
290                                    digits);
291
292         /* Units */
293         gtk_label_set_text (GTK_LABEL (position->units_label), units_string);
294
295 }
296
297 /*====================================================================*/
298 /* fill in position info only.                                        */
299 /*====================================================================*/
300 void
301 gl_wdgt_position_set_position (glWdgtPosition * position,
302                                gdouble x,
303                                gdouble y)
304 {
305         gdouble units_per_point;
306
307         gl_debug (DEBUG_WDGT, "START");
308
309         units_per_point = gl_prefs_get_units_per_point ();
310
311         gl_debug (DEBUG_WDGT, "units/point = %f", units_per_point);
312
313         /* Put everything in our display units */
314         x *= units_per_point;
315         y *= units_per_point;
316
317         /* update X/Y spin controls */
318         gtk_spin_button_set_value (GTK_SPIN_BUTTON (position->x_spin), x);
319         gtk_spin_button_set_value (GTK_SPIN_BUTTON (position->y_spin), y);
320
321         gl_debug (DEBUG_WDGT, "END");
322 }