]> git.sur5r.net Git - glabels/blob - src/object-editor-lsize-page.c
Upload to unstable
[glabels] / src / object-editor-lsize-page.c
1 /*
2  *  object-editor-lsize-page.c
3  *  Copyright (C) 2003-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "object-editor.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <math.h>
28
29 #include "prefs.h"
30 #include "builder-util.h"
31 #include "units-util.h"
32
33 #include "object-editor-private.h"
34
35 #include "debug.h"
36
37
38 /*===========================================*/
39 /* Private macros                            */
40 /*===========================================*/
41
42 #define LENGTH(x,y) sqrt( (x)*(x) + (y)*(y) )
43 #define ANGLE(x,y)  ( (180.0/G_PI)*atan2( -(y), (x) ) )
44 #define COMP_X(l,a) ( (l) * cos( (G_PI/180.0)*(a) ) )
45 #define COMP_Y(l,a) ( -(l) * sin( (G_PI/180.0)*(a) ) )
46                                                                                 
47
48 /*===========================================*/
49 /* Private data types                        */
50 /*===========================================*/
51
52
53 /*===========================================*/
54 /* Private globals                           */
55 /*===========================================*/
56
57
58 /*===========================================*/
59 /* Local function prototypes                 */
60 /*===========================================*/
61
62
63 /*--------------------------------------------------------------------------*/
64 /* PRIVATE.  Prepare line size page.                                        */
65 /*--------------------------------------------------------------------------*/
66 void
67 gl_object_editor_prepare_lsize_page (glObjectEditor       *editor)
68 {
69         lglUnits      units;
70         const gchar  *units_string;
71         gdouble       climb_rate;
72         gint          digits;
73
74         gl_debug (DEBUG_EDITOR, "START");
75
76         /* Extract widgets from XML tree. */
77         gl_builder_util_get_widgets (editor->priv->builder,
78                                      "lsize_page_vbox",     &editor->priv->lsize_page_vbox,
79                                      "lsize_r_spin",        &editor->priv->lsize_r_spin,
80                                      "lsize_theta_spin",    &editor->priv->lsize_theta_spin,
81                                      "lsize_r_units_label", &editor->priv->lsize_r_units_label,
82                                      NULL);
83
84         /* Get configuration information */
85         units = gl_prefs_model_get_units (gl_prefs);
86         units_string = lgl_units_get_name (units);
87         editor->priv->units_per_point = lgl_units_get_units_per_point (units);
88         climb_rate = gl_units_util_get_step_size (units);
89         digits = gl_units_util_get_precision (units);
90
91         /* Modify widgets based on configuration */
92         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin), digits);
93         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
94                                         climb_rate, 0);
95         gtk_label_set_text (GTK_LABEL(editor->priv->lsize_r_units_label), units_string);
96
97         /* Un-hide */
98         gtk_widget_show_all (editor->priv->lsize_page_vbox);
99
100         /* Connect signals */
101         g_signal_connect_swapped (G_OBJECT (editor->priv->lsize_r_spin),
102                                   "value-changed",
103                                   G_CALLBACK (gl_object_editor_size_changed_cb),
104                                   G_OBJECT (editor));
105         g_signal_connect_swapped (G_OBJECT (editor->priv->lsize_theta_spin),
106                                   "value-changed",
107                                   G_CALLBACK (gl_object_editor_size_changed_cb),
108                                   G_OBJECT (editor));
109
110         gl_debug (DEBUG_EDITOR, "END");
111 }
112
113
114 /*****************************************************************************/
115 /* Set line size.                                                            */
116 /*****************************************************************************/
117 void
118 gl_object_editor_set_lsize (glObjectEditor      *editor,
119                             gdouble              dx,
120                             gdouble              dy)
121 {
122         gdouble r, theta;
123
124         gl_debug (DEBUG_EDITOR, "START");
125
126
127         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->lsize_r_spin),
128                                          gl_object_editor_size_changed_cb, editor);
129         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->lsize_theta_spin),
130                                          gl_object_editor_size_changed_cb, editor);
131
132
133         /* save a copy in internal units */
134         editor->priv->dx = dx;
135         editor->priv->dy = dy;
136
137         /* convert internal units to displayed units */
138         gl_debug (DEBUG_EDITOR, "internal dx,dy = %g, %g", dx, dy);
139         dx *= editor->priv->units_per_point;
140         dy *= editor->priv->units_per_point;
141         gl_debug (DEBUG_EDITOR, "display dx,dy = %g, %g", dx, dy);
142
143         r     = LENGTH (dx, dy);
144         theta = ANGLE (dx, dy);
145
146         /* Set widget values */
147         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin), r);
148         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_theta_spin),
149                                    theta);
150
151
152         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->lsize_r_spin),
153                                            gl_object_editor_size_changed_cb, editor);
154         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->lsize_theta_spin),
155                                            gl_object_editor_size_changed_cb, editor);
156
157
158         gl_debug (DEBUG_EDITOR, "END");
159 }
160
161
162 /*****************************************************************************/
163 /* Set maximum line size.                                                    */
164 /*****************************************************************************/
165 void
166 gl_object_editor_set_max_lsize (glObjectEditor      *editor,
167                                 gdouble              dx_max,
168                                 gdouble              dy_max)
169 {
170         gdouble tmp;
171
172         gl_debug (DEBUG_EDITOR, "START");
173
174
175         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->lsize_r_spin),
176                                          gl_object_editor_size_changed_cb, editor);
177         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->lsize_theta_spin),
178                                          gl_object_editor_size_changed_cb, editor);
179
180
181         /* save a copy in internal units */
182         editor->priv->dx_max = dx_max;
183         editor->priv->dy_max = dy_max;
184
185         /* convert internal units to displayed units */
186         gl_debug (DEBUG_EDITOR, "internal dx_max,dy_max = %g, %g", dx_max, dy_max);
187         dx_max *= editor->priv->units_per_point;
188         dy_max *= editor->priv->units_per_point;
189         gl_debug (DEBUG_EDITOR, "display dx_max,dy_max = %g, %g", dx_max, dy_max);
190
191         /* Set widget values */
192         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin));
193         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin),
194                                    0.0, 2.0*LENGTH (dx_max, dy_max));
195         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin), tmp);
196
197
198         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->lsize_r_spin),
199                                            gl_object_editor_size_changed_cb, editor);
200         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->lsize_theta_spin),
201                                            gl_object_editor_size_changed_cb, editor);
202
203
204         gl_debug (DEBUG_EDITOR, "END");
205 }
206
207
208 /*****************************************************************************/
209 /* Query line size.                                                          */
210 /*****************************************************************************/
211 void
212 gl_object_editor_get_lsize (glObjectEditor      *editor,
213                             gdouble             *dx,
214                             gdouble             *dy)
215 {
216         gdouble r, theta;
217
218         gl_debug (DEBUG_EDITOR, "START");
219
220         /* Get values from widgets */
221         r = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin));
222         theta = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_theta_spin));
223
224         /* convert everything back to our internal units (points) */
225         r /= editor->priv->units_per_point;
226
227         *dx = COMP_X (r, theta);
228         *dy = COMP_Y (r, theta);
229
230         /* save a copy in internal units */
231         editor->priv->dx = *dx;
232         editor->priv->dy = *dy;
233
234         gl_debug (DEBUG_EDITOR, "END");
235 }
236
237
238 /*****************************************************************************/
239 /* PRIVATE. Prefs changed callback.  Update units related items.            */
240 /*****************************************************************************/
241 void
242 lsize_prefs_changed_cb (glObjectEditor *editor)
243 {
244         lglUnits      units;
245         const gchar  *units_string;
246         gdouble       climb_rate;
247         gint          digits;
248
249         gl_debug (DEBUG_EDITOR, "START");
250
251
252         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->lsize_r_spin),
253                                          gl_object_editor_size_changed_cb, editor);
254         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->lsize_theta_spin),
255                                          gl_object_editor_size_changed_cb, editor);
256
257
258         /* Get new configuration information */
259         units = gl_prefs_model_get_units (gl_prefs);
260         units_string = lgl_units_get_name (units);
261         editor->priv->units_per_point = lgl_units_get_units_per_point (units);
262         climb_rate = gl_units_util_get_step_size (units);
263         digits = gl_units_util_get_precision (units);
264
265         /* Update characteristics of r_spin */
266         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin), digits);
267         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin), climb_rate, 0);
268
269         /* Update r_units_label */
270         gtk_label_set_text (GTK_LABEL(editor->priv->lsize_r_units_label),
271                             units_string);
272
273         /* Update values of r_spin/theta_spin */
274         gl_object_editor_set_lsize (editor,
275                                     editor->priv->dx,
276                                     editor->priv->dy);
277         gl_object_editor_set_max_lsize (editor,
278                                         editor->priv->dx_max,
279                                         editor->priv->dy_max);
280
281
282         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->lsize_r_spin),
283                                            gl_object_editor_size_changed_cb, editor);
284         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->lsize_theta_spin),
285                                            gl_object_editor_size_changed_cb, editor);
286
287
288         gl_debug (DEBUG_EDITOR, "END");
289 }
290
291
292
293 /*
294  * Local Variables:       -- emacs
295  * mode: C                -- emacs
296  * c-basic-offset: 8      -- emacs
297  * tab-width: 8           -- emacs
298  * indent-tabs-mode: nil  -- emacs
299  * End:                   -- emacs
300  */