2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * wdgt_position.c: position properties widget module
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
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.
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.
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
25 #include "wdgt-position.h"
31 /*===========================================*/
33 /*===========================================*/
40 typedef void (*glWdgtPositionSignal) (GObject * object, gpointer data);
42 /*===========================================*/
44 /*===========================================*/
46 static GObjectClass *parent_class;
48 static gint wdgt_position_signals[LAST_SIGNAL] = { 0 };
50 /*===========================================*/
51 /* Local function prototypes */
52 /*===========================================*/
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,
59 static void changed_cb (glWdgtPosition * position);
61 /*================================================================*/
62 /* Boilerplate Object stuff. */
63 /*================================================================*/
65 gl_wdgt_position_get_type (void)
67 static guint wdgt_position_type = 0;
69 if (!wdgt_position_type) {
70 GTypeInfo wdgt_position_info = {
71 sizeof (glWdgtPositionClass),
74 (GClassInitFunc) gl_wdgt_position_class_init,
77 sizeof (glWdgtPosition),
79 (GInstanceInitFunc) gl_wdgt_position_instance_init,
82 wdgt_position_type = g_type_register_static (gtk_vbox_get_type (),
88 return wdgt_position_type;
92 gl_wdgt_position_class_init (glWdgtPositionClass * class)
94 GObjectClass *object_class;
96 object_class = (GObjectClass *) class;
98 parent_class = gtk_type_class (gtk_vbox_get_type ());
100 object_class->finalize = gl_wdgt_position_finalize;
102 wdgt_position_signals[CHANGED] =
103 g_signal_new ("changed",
104 G_OBJECT_CLASS_TYPE(object_class),
106 G_STRUCT_OFFSET (glWdgtPositionClass, changed),
108 gl_marshal_VOID__VOID,
114 gl_wdgt_position_instance_init (glWdgtPosition * position)
116 position->x_spin = NULL;
117 position->y_spin = NULL;
121 gl_wdgt_position_finalize (GObject * object)
123 glWdgtPosition *position;
125 g_return_if_fail (object != NULL);
126 g_return_if_fail (GL_IS_WDGT_POSITION (object));
128 position = GL_WDGT_POSITION (object);
130 G_OBJECT_CLASS (parent_class)->finalize (object);
134 gl_wdgt_position_new (gchar * label)
136 glWdgtPosition *position;
138 position = g_object_new (gl_wdgt_position_get_type (), NULL);
140 gl_wdgt_position_construct (position, label);
142 return GTK_WIDGET (position);
145 /*============================================================*/
146 /* Construct composite widget. */
147 /*============================================================*/
149 gl_wdgt_position_construct (glWdgtPosition * position,
152 GtkWidget *wvbox, *wframe, *wtable, *wlabel;
153 GtkObject *x_adjust, *y_adjust;
154 const gchar *units_string;
155 gdouble units_per_point, climb_rate;
158 gl_debug (DEBUG_WDGT, "START");
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 ();
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);
170 wvbox = GTK_WIDGET (position);
172 wframe = gtk_frame_new (label);
173 gtk_box_pack_start (GTK_BOX (wvbox), wframe, FALSE, FALSE, 0);
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);
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);
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),
190 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (position->x_spin),
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,
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);
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),
208 gtk_spin_button_set_snap_to_ticks (GTK_SPIN_BUTTON (position->y_spin),
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,
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);
223 gl_debug (DEBUG_WDGT, "END");
226 /*--------------------------------------------------------------------------*/
227 /* PRIVATE. Callback for when any control in the widget has changed. */
228 /*--------------------------------------------------------------------------*/
230 changed_cb (glWdgtPosition * position)
232 /* Emit our "changed" signal */
233 g_signal_emit (G_OBJECT (position), wdgt_position_signals[CHANGED], 0);
236 /*====================================================================*/
237 /* query values from controls. */
238 /*====================================================================*/
240 gl_wdgt_position_get_position (glWdgtPosition * position,
244 gdouble units_per_point;
246 units_per_point = gl_prefs_get_units_per_point ();
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));
251 /* convert everything back to our internal units (points) */
252 *x /= units_per_point;
253 *y /= units_per_point;
256 /*====================================================================*/
257 /* fill in values and ranges for controls. */
258 /*====================================================================*/
260 gl_wdgt_position_set_params (glWdgtPosition * position,
266 GtkObject *x_adjust, *y_adjust;
267 const gchar *units_string;
268 gdouble units_per_point, climb_rate;
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 ();
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;
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,
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,
293 gtk_label_set_text (GTK_LABEL (position->units_label), units_string);
297 /*====================================================================*/
298 /* fill in position info only. */
299 /*====================================================================*/
301 gl_wdgt_position_set_position (glWdgtPosition * position,
305 gdouble units_per_point;
307 gl_debug (DEBUG_WDGT, "START");
309 units_per_point = gl_prefs_get_units_per_point ();
311 gl_debug (DEBUG_WDGT, "units/point = %f", units_per_point);
313 /* Put everything in our display units */
314 x *= units_per_point;
315 y *= units_per_point;
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);
321 gl_debug (DEBUG_WDGT, "END");