]> git.sur5r.net Git - glabels/blob - src/object-editor-position-page.c
Imported Upstream version 2.2.8
[glabels] / src / object-editor-position-page.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  object-editor.c:  object properties editor module
7  *
8  *  Copyright (C) 2003  Jim Evins <evins@snaught.com>.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24 #include <config.h>
25
26 #include "object-editor.h"
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtklabel.h>
30 #include <gtk/gtkspinbutton.h>
31 #include <math.h>
32
33 #include "prefs.h"
34
35 #include "object-editor-private.h"
36
37 #include "debug.h"
38
39 /*===========================================*/
40 /* Private macros                            */
41 /*===========================================*/
42
43 /*===========================================*/
44 /* Private data types                        */
45 /*===========================================*/
46
47 /*===========================================*/
48 /* Private globals                           */
49 /*===========================================*/
50
51 /*===========================================*/
52 /* Local function prototypes                 */
53 /*===========================================*/
54
55
56 /*--------------------------------------------------------------------------*/
57 /* PRIVATE.  Prepare position page.                                         */
58 /*--------------------------------------------------------------------------*/
59 void
60 gl_object_editor_prepare_position_page (glObjectEditor *editor)
61 {
62         const gchar  *units_string;
63         gdouble       climb_rate;
64         gint          digits;
65
66         gl_debug (DEBUG_EDITOR, "START");
67
68         /* Extract widgets from XML tree. */
69         editor->priv->pos_page_vbox = glade_xml_get_widget (editor->priv->gui,
70                                                               "pos_page_vbox");
71         editor->priv->pos_x_spin    = glade_xml_get_widget (editor->priv->gui,
72                                                               "pos_x_spin");
73         editor->priv->pos_y_spin    = glade_xml_get_widget (editor->priv->gui,
74                                                               "pos_y_spin");
75         editor->priv->pos_x_units_label = glade_xml_get_widget (editor->priv->gui,
76                                                                   "pos_x_units_label");
77         editor->priv->pos_y_units_label = glade_xml_get_widget (editor->priv->gui,
78                                                                   "pos_y_units_label");
79
80         /* Get configuration information */
81         units_string = gl_prefs_get_units_string ();
82         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
83         climb_rate = gl_prefs_get_units_step_size ();
84         digits = gl_prefs_get_units_precision ();
85
86         /* Modify widgets based on configuration */
87         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->pos_x_spin), digits);
88         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->pos_x_spin),
89                                         climb_rate, 10.0*climb_rate);
90         gtk_label_set_text (GTK_LABEL(editor->priv->pos_x_units_label), units_string);
91         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->pos_y_spin), digits);
92         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->pos_y_spin),
93                                         climb_rate, 10.0*climb_rate);
94         gtk_label_set_text (GTK_LABEL(editor->priv->pos_y_units_label), units_string);
95
96         /* Un-hide */
97         gtk_widget_show_all (editor->priv->pos_page_vbox);
98
99         /* Connect signals */
100         g_signal_connect_swapped (G_OBJECT (editor->priv->pos_x_spin),
101                                   "changed",
102                                   G_CALLBACK (gl_object_editor_changed_cb),
103                                   G_OBJECT (editor));
104         g_signal_connect_swapped (G_OBJECT (editor->priv->pos_y_spin),
105                                   "changed",
106                                   G_CALLBACK (gl_object_editor_changed_cb),
107                                   G_OBJECT (editor));
108
109         gl_debug (DEBUG_EDITOR, "END");
110 }
111
112 /*****************************************************************************/
113 /* Set position.                                                             */
114 /*****************************************************************************/
115 void
116 gl_object_editor_set_position (glObjectEditor      *editor,
117                                  gdouble                x,
118                                  gdouble                y)
119 {
120         gl_debug (DEBUG_EDITOR, "START");
121
122         editor->priv->stop_signals = TRUE;
123
124         /* save a copy in internal units */
125         editor->priv->x = x;
126         editor->priv->y = y;
127
128         /* convert internal units to displayed units */
129         gl_debug (DEBUG_EDITOR, "internal x,y = %g, %g", x, y);
130         x *= editor->priv->units_per_point;
131         y *= editor->priv->units_per_point;
132         gl_debug (DEBUG_EDITOR, "display x,y = %g, %g", x, y);
133
134         /* Set widget values */
135         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->pos_x_spin), x);
136         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->pos_y_spin), y);
137
138         editor->priv->stop_signals = FALSE;
139
140         gl_debug (DEBUG_EDITOR, "END");
141 }
142
143 /*****************************************************************************/
144 /* Set maximum position.                                                     */
145 /*****************************************************************************/
146 void
147 gl_object_editor_set_max_position (glObjectEditor      *editor,
148                                      gdouble                x_max,
149                                      gdouble                y_max)
150 {
151         gdouble tmp;
152
153         gl_debug (DEBUG_EDITOR, "START");
154
155         editor->priv->stop_signals = TRUE;
156
157         /* save a copy in internal units */
158         editor->priv->x_max = x_max;
159         editor->priv->y_max = y_max;
160
161         /* convert internal units to displayed units */
162         gl_debug (DEBUG_EDITOR, "internal x_max,y_max = %g, %g", x_max, y_max);
163         x_max *= editor->priv->units_per_point;
164         y_max *= editor->priv->units_per_point;
165         gl_debug (DEBUG_EDITOR, "display x_max,y_max = %g, %g", x_max, y_max);
166
167         /* Set widget values */
168         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->pos_x_spin));
169         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->pos_x_spin),
170                                    -x_max, 2.0*x_max);
171         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->pos_x_spin), tmp);
172         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->pos_y_spin));
173         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->pos_y_spin),
174                                    -y_max, 2.0*y_max);
175         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->pos_y_spin), tmp);
176
177         editor->priv->stop_signals = FALSE;
178
179         gl_debug (DEBUG_EDITOR, "END");
180 }
181
182 /*****************************************************************************/
183 /* Query position.                                                           */
184 /*****************************************************************************/
185 void
186 gl_object_editor_get_position (glObjectEditor      *editor,
187                                  gdouble               *x,
188                                  gdouble               *y)
189 {
190         gl_debug (DEBUG_EDITOR, "START");
191
192         /* Get values from widgets */
193         *x = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->pos_x_spin));
194         *y = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->pos_y_spin));
195
196         /* convert everything back to our internal units (points) */
197         *x /= editor->priv->units_per_point;
198         *y /= editor->priv->units_per_point;
199
200         /* save a copy in internal units */
201         editor->priv->x = *x;
202         editor->priv->y = *y;
203
204         gl_debug (DEBUG_EDITOR, "END");
205 }
206
207 /*****************************************************************************/
208 /* PRIVATE. Prefs changed callback.  Update units related items.            */
209 /*****************************************************************************/
210 void
211 position_prefs_changed_cb (glObjectEditor *editor)
212 {
213         const gchar  *units_string;
214         gdouble       climb_rate;
215         gint          digits;
216
217         gl_debug (DEBUG_EDITOR, "START");
218
219         /* Get new configuration information */
220         units_string = gl_prefs_get_units_string ();
221         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
222         climb_rate = gl_prefs_get_units_step_size ();
223         digits = gl_prefs_get_units_precision ();
224
225         /* Update characteristics of x_spin/y_spin */
226         editor->priv->stop_signals = TRUE;
227         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->pos_x_spin),
228                                     digits);
229         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->pos_y_spin),
230                                     digits);
231         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->pos_x_spin),
232                                         climb_rate, 10.0*climb_rate);
233         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->pos_y_spin),
234                                         climb_rate, 10.0*climb_rate);
235         editor->priv->stop_signals = FALSE;
236
237         /* Update units_labels */
238         gtk_label_set_text (GTK_LABEL(editor->priv->pos_x_units_label),
239                             units_string);
240         gtk_label_set_text (GTK_LABEL(editor->priv->pos_y_units_label),
241                             units_string);
242
243         /* Update values of x_spin/y_spin */
244         gl_object_editor_set_position (editor,
245                                        editor->priv->x,
246                                        editor->priv->y);
247         gl_object_editor_set_max_position (editor,
248                                            editor->priv->x_max,
249                                            editor->priv->y_max);
250
251         gl_debug (DEBUG_EDITOR, "END");
252 }
253