]> git.sur5r.net Git - glabels/blob - src/object-editor-lsize-page.c
Organized master branch to be top-level directory for glabels, instead of
[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
32 #include "object-editor-private.h"
33
34 #include "debug.h"
35
36
37 /*===========================================*/
38 /* Private macros                            */
39 /*===========================================*/
40
41 #define LENGTH(x,y) sqrt( (x)*(x) + (y)*(y) )
42 #define ANGLE(x,y)  ( (180.0/G_PI)*atan2( -(y), (x) ) )
43 #define COMP_X(l,a) ( (l) * cos( (G_PI/180.0)*(a) ) )
44 #define COMP_Y(l,a) ( -(l) * sin( (G_PI/180.0)*(a) ) )
45                                                                                 
46
47 /*===========================================*/
48 /* Private data types                        */
49 /*===========================================*/
50
51
52 /*===========================================*/
53 /* Private globals                           */
54 /*===========================================*/
55
56
57 /*===========================================*/
58 /* Local function prototypes                 */
59 /*===========================================*/
60
61
62 /*--------------------------------------------------------------------------*/
63 /* PRIVATE.  Prepare line size page.                                        */
64 /*--------------------------------------------------------------------------*/
65 void
66 gl_object_editor_prepare_lsize_page (glObjectEditor       *editor)
67 {
68         const gchar  *units_string;
69         gdouble       climb_rate;
70         gint          digits;
71
72         gl_debug (DEBUG_EDITOR, "START");
73
74         /* Extract widgets from XML tree. */
75         gl_builder_util_get_widgets (editor->priv->builder,
76                                      "lsize_page_vbox",     &editor->priv->lsize_page_vbox,
77                                      "lsize_r_spin",        &editor->priv->lsize_r_spin,
78                                      "lsize_theta_spin",    &editor->priv->lsize_theta_spin,
79                                      "lsize_r_units_label", &editor->priv->lsize_r_units_label,
80                                      NULL);
81
82         /* Get configuration information */
83         units_string = gl_prefs_get_units_string ();
84         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
85         climb_rate = gl_prefs_get_units_step_size ();
86         digits = gl_prefs_get_units_precision ();
87
88         /* Modify widgets based on configuration */
89         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin), digits);
90         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
91                                         climb_rate, 0);
92         gtk_label_set_text (GTK_LABEL(editor->priv->lsize_r_units_label), units_string);
93
94         /* Un-hide */
95         gtk_widget_show_all (editor->priv->lsize_page_vbox);
96
97         /* Connect signals */
98         g_signal_connect_swapped (G_OBJECT (editor->priv->lsize_r_spin),
99                                   "changed",
100                                   G_CALLBACK (gl_object_editor_changed_cb),
101                                   G_OBJECT (editor));
102         g_signal_connect_swapped (G_OBJECT (editor->priv->lsize_theta_spin),
103                                   "changed",
104                                   G_CALLBACK (gl_object_editor_changed_cb),
105                                   G_OBJECT (editor));
106
107         gl_debug (DEBUG_EDITOR, "END");
108 }
109
110
111 /*****************************************************************************/
112 /* Set line size.                                                            */
113 /*****************************************************************************/
114 void
115 gl_object_editor_set_lsize (glObjectEditor      *editor,
116                             gdouble              dx,
117                             gdouble              dy)
118 {
119         gdouble r, theta;
120
121         gl_debug (DEBUG_EDITOR, "START");
122
123         editor->priv->stop_signals = TRUE;
124
125         /* save a copy in internal units */
126         editor->priv->dx = dx;
127         editor->priv->dy = dy;
128
129         /* convert internal units to displayed units */
130         gl_debug (DEBUG_EDITOR, "internal dx,dy = %g, %g", dx, dy);
131         dx *= editor->priv->units_per_point;
132         dy *= editor->priv->units_per_point;
133         gl_debug (DEBUG_EDITOR, "display dx,dy = %g, %g", dx, dy);
134
135         r     = LENGTH (dx, dy);
136         theta = ANGLE (dx, dy);
137
138         /* Set widget values */
139         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin), r);
140         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_theta_spin),
141                                    theta);
142
143         editor->priv->stop_signals = FALSE;
144
145         gl_debug (DEBUG_EDITOR, "END");
146 }
147
148
149 /*****************************************************************************/
150 /* Set maximum line size.                                                    */
151 /*****************************************************************************/
152 void
153 gl_object_editor_set_max_lsize (glObjectEditor      *editor,
154                                 gdouble              dx_max,
155                                 gdouble              dy_max)
156 {
157         gdouble tmp;
158
159         gl_debug (DEBUG_EDITOR, "START");
160
161         if (editor->priv->lsize_page_vbox)
162         {
163
164                 editor->priv->stop_signals = TRUE;
165
166                 /* save a copy in internal units */
167                 editor->priv->dx_max = dx_max;
168                 editor->priv->dy_max = dy_max;
169
170                 /* convert internal units to displayed units */
171                 gl_debug (DEBUG_EDITOR, "internal dx_max,dy_max = %g, %g", dx_max, dy_max);
172                 dx_max *= editor->priv->units_per_point;
173                 dy_max *= editor->priv->units_per_point;
174                 gl_debug (DEBUG_EDITOR, "display dx_max,dy_max = %g, %g", dx_max, dy_max);
175
176                 /* Set widget values */
177                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin));
178                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin),
179                                            0.0, 2.0*LENGTH (dx_max, dy_max));
180                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin), tmp);
181
182                 editor->priv->stop_signals = FALSE;
183
184         }
185
186         gl_debug (DEBUG_EDITOR, "END");
187 }
188
189
190 /*****************************************************************************/
191 /* Query line size.                                                          */
192 /*****************************************************************************/
193 void
194 gl_object_editor_get_lsize (glObjectEditor      *editor,
195                             gdouble             *dx,
196                             gdouble             *dy)
197 {
198         gdouble r, theta;
199
200         gl_debug (DEBUG_EDITOR, "START");
201
202         /* Get values from widgets */
203         r = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin));
204         theta = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_theta_spin));
205
206         /* convert everything back to our internal units (points) */
207         r /= editor->priv->units_per_point;
208
209         *dx = COMP_X (r, theta);
210         *dy = COMP_Y (r, theta);
211
212         /* save a copy in internal units */
213         editor->priv->dx = *dx;
214         editor->priv->dy = *dy;
215
216         gl_debug (DEBUG_EDITOR, "END");
217 }
218
219
220 /*****************************************************************************/
221 /* PRIVATE. Prefs changed callback.  Update units related items.            */
222 /*****************************************************************************/
223 void
224 lsize_prefs_changed_cb (glObjectEditor *editor)
225 {
226         const gchar  *units_string;
227         gdouble       climb_rate;
228         gint          digits;
229
230         gl_debug (DEBUG_EDITOR, "START");
231
232         /* Get new configuration information */
233         units_string = gl_prefs_get_units_string ();
234         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
235         climb_rate = gl_prefs_get_units_step_size ();
236         digits = gl_prefs_get_units_precision ();
237
238         /* Update characteristics of r_spin */
239         editor->priv->stop_signals = TRUE;
240         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
241                                     digits);
242         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
243                                         climb_rate, 0);
244         editor->priv->stop_signals = FALSE;
245
246         /* Update r_units_label */
247         gtk_label_set_text (GTK_LABEL(editor->priv->lsize_r_units_label),
248                             units_string);
249
250         /* Update values of r_spin/theta_spin */
251         gl_object_editor_set_lsize (editor,
252                                     editor->priv->dx,
253                                     editor->priv->dy);
254         gl_object_editor_set_max_lsize (editor,
255                                         editor->priv->dx_max,
256                                         editor->priv->dy_max);
257
258         gl_debug (DEBUG_EDITOR, "END");
259 }
260
261
262
263 /*
264  * Local Variables:       -- emacs
265  * mode: C                -- emacs
266  * c-basic-offset: 8      -- emacs
267  * tab-width: 8           -- emacs
268  * indent-tabs-mode: nil  -- emacs
269  * End:                   -- emacs
270  */